2013-06-21 86 views
2

他我对Java(这不是新的)noobot,我找不到一个很好的教程。我使用jTable来显示填充MySQL数据库数据的表格。JTable单元格更改的JAVA更新数据库

到目前为止好,我拿到表:

table preview

标准可以单击表格单元格,并将其与能与新的东西来填充更改为文本字段。你们都知道,但我怎样才能使用它来更新数据库中的值?

我的代码:

import [...]; 
public class table extends JPanel { 
    public String table; 
    private Database db; 

    public table(String tablename, Database db){ 

     try { 
      table = tablename; 
      this.db = db; 

      //Get table with and height 
      ResultSet res = db.query("SELECT COUNT(*) FROM `"+table+"`"); 
      ResultSet res2 = db.query("SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = '"+table+"'"); 

      int rows = 0; 
      int collums = 0; 

      res.next(); 
      res2.next(); 

      rows = res.getInt(1); 
      collums = res2.getInt(1); 

      //Get table column names and set then in array 
      ResultSet clom = db.query("DESCRIBE `"+table+"`"); 

      String[] columnNames = new String[collums]; 

      int s = 0; 

      while(clom.next()){ 
       columnNames[s] = clom.getString(1); 
       s++; 
      } 

      //get table data and put in array 
      Object[][] data = new Object[rows][collums]; 

      ResultSet result = db.query("SELECT * FROM `"+table+"`"); 

      int q = 0; 

      while(result.next()){ 
       for(int a=0; a<= (collums - 1); a++){ 
        data[q][a] = result.getString(a + 1); 
        //System.out.println(q + " - " + a); 
       } 
       q++; 
      } 

      //Make Jtable of the db result form the two array's 
      final JTable table = new JTable(data, columnNames); 
      table.setPreferredScrollableViewportSize(new Dimension(500, 70)); 
      table.setFillsViewportHeight(true); 

      // do some event listening for cell change 

      JScrollPane scrollPane = new JScrollPane(table); 
      JFrame frame = new JFrame("table editor"); 
      scrollPane.setOpaque(true); 
      frame.setContentPane(scrollPane); 
      frame.pack(); 
      frame.setSize(600, 800); 
      frame.setVisible(true); 
     } catch (SQLException e1) { 
      e1.printStackTrace(); 
     } 
    } 
} 

我想我需要绑定某种表监听,当有新的变化我把表的所有值,并用查询更新它们。

我该怎么做?

这是我的伪代码:

table.bindCellEventListner(callback(t){ 
    Array row = t.getAllValuesAsArrayOfRow(); 

    String data = ""; 

    int f = 0 
    while(row.next()){ 
     data .= "`"+clom[f]+"` = '"+row[f]+"'," 
     f++; 
    } 
    data.delLastChar(); 
    db.query("UPDATE `"+table+"` SET "+data+" WHERE `id` ="+row[0]+";"); 

}); 

回答

8

第1步:这将是对整个事情的方式更容易,如果你让你的表从现在延长TableModel的(如果你还没有的话)。所以,如果你需要帮助,只是问。

第2步:在表模型中,您需要启用单元格为可编辑。在你创建的TableModel类中,你需要添加这些方法。 注:在代码块文本内容自动为自动换行

public boolean isCellEditable(int row, int col) 
     { return true; } 
    public void setValueAt(Object value, int row, int col) { 
     rowData[row][col] = value; 
     fireTableCellUpdated(row, col); 
    } 

第3步:您将在我们火了一个名为fireTableCellUpdated方法第二种方法见。所以在这里我们可以捕捉到它改变的用途。你需要添加一个TableModelListener到你的表来捕捉这个。

mytable.getModel()。addTableModelListener(yourClass);

而在你决定实现TableModelListener的类中,你需要这个 view plaincopy to clipboardprint? 注:在代码块文本内容自动为自动换行

public void tableChanged(TableModelEvent e) { 
     int row = e.getFirstRow(); 
     int column = e.getColumn(); 
     TableModel model = (TableModel)e.getSource(); 
     Object data = model.getValueAt(row, column); 

//now you have the data in the cell and the place in the grid where the 

//cell is so you can use the data as you want 
    } 
+0

,看起来像它,你可以播种了我如何做一个TableModel的像你描述? – botenvouwer

+0

他能帮你创建一个扩展表模型吗? – botenvouwer

+0

好的,我做到了。谢谢! – botenvouwer