2012-11-24 70 views
1

真的在我的項目上遇到了麻煩,我對java很陌生,對數據庫來說真的很陌生。我在我的代碼中所做的是用一堆單詞填充「數據」對象,但我想要做的是用來自我創建的數據庫的信息填充這些框架。我一直在谷歌搜索和搜索幾天,並沒有發現任何有助於我理解我需要做的事情。任何幫助將不勝感激。從MySQL提交一個Jframe

import java.awt.*; 
import javax.swing.*; 
import javax.swing.border.*; 
import javax.swing.table.*; 
import java.awt.event.*; 

public class Project3 extends JFrame { 
private JPanel contentPane; 
private JTable table_1; 
private JScrollPane scrollPane; 
String[] columnNames = { "Restaurant", "Dish", "Type", "Price", "Rating" }; 
Object[][] data = { 
     { "Nemo", "Vesuvio", "Pizza", new String("65kr"), new Integer(7) }, 
     { "John", "Doe", "Rowing", new Integer(3), new Boolean(true) }, 
     { "Sue", "Black", "Knitting", new Integer(2), new Boolean(false) }, 
     { "Jane", "White", "Speed reading", new Integer(20), 
       new Boolean(true) }, 
     { "Joe", "Brown", "Pool", new Integer(10), new Boolean(false) } }; 

/** 
* Launch the application. 
*/ 
public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      try { 
       Project3 frame = new Project3(); 
       frame.setVisible(true); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 
} 

/** 
* Create the frame. 
*/ 
public Project3() { 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setBounds(100, 100, 800, 600); 
    contentPane = new JPanel(); 
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
    contentPane.setLayout(new BorderLayout(0, 0)); 
    setContentPane(contentPane); 
    JPanel panel = new JPanel(); 
    contentPane.add(panel, BorderLayout.CENTER); 
    panel.setLayout(null); 
    scrollPane = new JScrollPane(); 
    scrollPane.setBounds(139, 162, 469, 420); 
    panel.add(scrollPane); 
    table_1 = new JTable(data, columnNames); 
    scrollPane.setViewportView(table_1); 
    table_1.setFillsViewportHeight(true); 
    table_1.setModel(new DefaultTableModel(new Object[][] { 
      { "Nemo", "blah", "Pizza", "65kr", new Integer(7) }, 
      { "Nemo", "blah", "Rowing", "1 000 000kr", Boolean.TRUE }, 
      { "Sue", "blah", "Knitting", new Integer(2), Boolean.FALSE }, 
      { "Jane", "blah", "Speed reading", new Integer(20), 
        Boolean.TRUE }, 
      { "Joe", "Brown", "Pool", new Integer(10), Boolean.FALSE }, 
      { null, null, null, null, null }, 
      { null, null, null, null, null }, 
      { null, null, null, null, null }, 
      { null, null, null, null, null }, 
      { null, null, null, null, null }, 
      { null, null, null, null, null }, 
      { null, null, null, null, null }, 
      { null, null, null, null, null }, 
      { null, null, null, null, null }, 
      { null, null, null, null, null }, 
      { null, null, null, null, null }, 
      { null, null, null, null, null }, 
      { null, null, null, null, null }, 
      { null, null, null, null, null }, 
      { null, null, null, null, null }, 
      { null, null, null, null, null }, 
      { null, null, null, null, null }, 
      { null, null, null, null, null }, 
      { null, null, null, null, null }, 
      { null, null, null, null, null }, 
      { null, null, null, null, null }, 
      { null, null, null, null, null }, }, new String[] { 
      "Restaurant", "Dish", "Type", "Price", "Rating" })); 
    JButton btnChangeadd = new JButton("Change/Add"); 
    btnChangeadd.addMouseListener(new MouseAdapter() { 
     @Override 
     public void mouseClicked(MouseEvent e) { 
      // CALL CHANGE METHOD HERE 
     } 
    }); 
    btnChangeadd.setFont(new Font("Lucida Grande", Font.PLAIN, 16)); 
    btnChangeadd.setBounds(143, 104, 175, 42); 
    panel.add(btnChangeadd); 
    JLabel lblNewLabel = new JLabel("Welcome RestaurantID"); 
    lblNewLabel.setFont(new Font("Lucida Grande", Font.PLAIN, 24)); 
    lblNewLabel.setBounds(264, 56, 260, 36); 
    panel.add(lblNewLabel); 
    JButton button = new JButton("+"); 
    button.addMouseListener(new MouseAdapter() { 
     @Override 
     public void mouseClicked(MouseEvent e) { 
      // CALL SOMETHING HERE IF YOU WANT 
     } 
    }); 
    button.setBounds(66, 248, 40, 29); 
    panel.add(button); 
    JButton button_1 = new JButton("-"); 
    button_1.addMouseListener(new MouseAdapter() { 
     @Override 
     public void mouseClicked(MouseEvent e) { 

     } 
    }); 
    button_1.setBounds(66, 289, 40, 29); 
    panel.add(button_1); 
    JButton btnDelete = new JButton("Delete"); 
    btnDelete.addMouseListener(new MouseAdapter() { 
     @Override 
     public void mouseClicked(MouseEvent e) { 
      //DELETE METHOD 
     } 
    }); 
    btnDelete.setFont(new Font("Lucida Grande", Font.PLAIN, 16)); 
    btnDelete.setBounds(344, 104, 191, 42); 
    panel.add(btnDelete); 
    JButton btnLogOut = new JButton("Log out"); 
    btnLogOut.addMouseListener(new MouseAdapter() { 
     @Override 
     public void mouseClicked(MouseEvent e) { 
     } 
    }); 
    btnLogOut.setBounds(667, 6, 117, 29); 
    panel.add(btnLogOut); 
} 

}

+0

你的問題是什麼,你卡在哪裏? –

+0

看到問題是我自己填充了數據,並且是數據庫的完全新手我無法爲我的數據生活找出我需要放在這個字段中使其從數據庫中取出而不必手動將其存入 – Cameronaut

回答

3

開始與JDBC tutorial,然後研究文章Table From Database。此外,

  • 使用常量或工廠時可用,而不是無謂的構造函數,例如:

    {"Nemo", "Vesuvio", "Pizza", "65kr", 7}, 
    {"John", "Doe", "Rowing", 3, true}, 
    {"Sue", "Black", "Knitting", Integer.valueOf(3), Boolean.FALSE}, 
    
  • 使用@Override註解,以避免不小心超載:

    @Override 
    public void run() { 
        ... 
    } 
    
  • 開始使用Java Collections Framework來擁抱對象方向,而不是數組。請注意引用的示例如何指定形式參數,如List<T>,其中List是接口類型,而T是泛型類型參數。

+0

我還會添加對象化數據(使數據庫中的數據成爲對象表示形式),而不是依賴數組。它使生活變得更容易,特別是當涉及到處理表格時,因爲每個對象都變成一排 - 恕我直言 – MadProgrammer

+0

@MadProgrammer:好的建議;謝謝。看看我是否抓住了你的建議的本質;編輯_ad lib_。 – trashgod