使用sql更新jTable中的单元格

时间:2011-04-02 16:37:52

标签: java sql jtable

如标题

我想知道如何使用sql

更新jTable中的特定单元格(在java中)

这是我的尝试,但它不起作用

public void actionPerformed(ActionEvent e) {
    int sel_tab=jTable1.getSelectedRow();

    myDbConnection dbconnect;
    dbconnect = new myDbConnection();
        ResultSet resultSet =null;
        resultSet = dbconnect.excuteQuery("SELECT id, area,location, status1 FROM pledges ");     
   if(e.getSource()==jButton1){
        try {
        String n = resultSet.getString("id");
        String a = resultSet.getString("area");
        String p = resultSet.getString("location");
        String se = resultSet.getString("status1");
            dbconnect.excuteUpdate("update pledges SET area="+ a +" whare id= "+sel_tab+"");
        } catch (SQLException ex) {;}
   }
}

提前谢谢

注意:我正在研究netbeans

1 个答案:

答案 0 :(得分:1)

最后我找到了正确的答案

以下代码将解释每件事

  public void actionPerformed(ActionEvent e) {


   if(e.getSource()==jButton1){



               jTable1.getModel().addTableModelListener(new TableModelListener() {
  public void tableChanged(TableModelEvent e) {
      myDbConnection dbconnect;
    dbconnect = new myDbConnection();
         ResultSet resultSet =null;
        resultSet = dbconnect.excuteQuery("SELECT id, area,location, status1 FROM pledges ");
       int row = e.getFirstRow();
    int column = e.getColumn();
    TableModel model = (TableModel)e.getSource();
    String columnName = model.getColumnName(column);
    Object data = model.getValueAt(row, column);
    int viewRow = jTable1.getSelectedRow(); // view row with sort
    int modelRow = jTable1.convertRowIndexToModel(viewRow); // convert
    dbconnect.excuteUpdate("update pledges set area ='" + data + "' where id = '"+jTable1.getModel().getValueAt(modelRow,0) +"'"  );
      System.out.println("Table value changed "+data);
  }
});

感谢每个人帮助我