Java 从动态jtable更新数据库

Java 从动态jtable更新数据库,java,sql,database,jtable,Java,Sql,Database,Jtable,我正在尝试从动态JTable更新数据库。这是我的密码 try { //open connection... conn = javaConnect.ConnectDb(); //select the qualifications table row for the selected staffID String sql2 = "select * from QualificationsTable where qualID =" + theRowID; pStmt = c

我正在尝试从动态JTable更新数据库。这是我的密码

try {
   //open connection...
   conn = javaConnect.ConnectDb();
   //select the qualifications table row for the selected staffID
   String sql2 = "select * from QualificationsTable where qualID =" + theRowID;
   pStmt = conn.prepareStatement(sql2);
   ResultSet rs2 = pStmt.executeQuery();
   //check if QualificationsTable has content on that row...
   if (rs2.next()) {
      //it has content update...
      //get the model for the qual table...
      DefaultTableModel tModel = (DefaultTableModel) qualTable.getModel();
      for (int i = 0; i < tModel.getRowCount(); i++) {

         //get inputs from the tables
         String qualification = tModel.getValueAt(i, 0).toString();
         String yearAttained = tModel.getValueAt(i, 1).toString();

         //sql query for updating qualifications table...
         String sql3 = "update QualificationsTable set qualifications = ?, yearAttained = ? where qualID = ?";
         pStmt = conn.prepareStatement(sql3);
         //set the pareameters...
         pStmt.setString(1, qualification);
         pStmt.setString(2, yearAttained);
         pStmt.setInt(3, theRowID);
         //execute the prepared statement...
         pStmt.execute();
         // dbStatement.executeUpdate("INSERT INTO tableName VALUES('"+item+"','"+quant+"','"+unit+"','"+tot+"')");
      }
      //close connection
      conn.close();

      JOptionPane.showMessageDialog(null, "Qualifications updated successfully!", "Success", INFORMATION_MESSAGE);
   } else {
      //it doesnt have content insert...
      //get the model for the qual table...
      DefaultTableModel tModel = (DefaultTableModel) qualTable.getModel();

      for (int i = 0; i < tModel.getRowCount(); i++) {                    
          //System.out.println(tModel.getSelectedColumn()+tModel.getSelectedRow());

          //get inputs from the tables
          String qualification = tModel.getValueAt(i, 0).toString();
          String yearAttained = tModel.getValueAt(i, 1).toString();

          //sql query for storing into QualificationsTable
          String sql3 = "insert into QualificationsTable (qualifications,yearAttained,qualID) "
                   + "values (?,?,?)";
          pStmt = conn.prepareStatement(sql3);
          //set the parameters...
          pStmt.setString(1, qualification);
          pStmt.setString(2, yearAttained);
          pStmt.setInt(3, theRowID);
          //execute the prepared statement...
          pStmt.execute();

       }
       //close connection
       conn.close();
       JOptionPane.showMessageDialog(null, "Qualifications saved successfully!", "Success", INFORMATION_MESSAGE);
    }
 } catch (SQLException ex) {
    Logger.getLogger(StoreInfo.class.getName()).log(Level.SEVERE, null, ex);
 } catch(NullPointerException nfe){
    JOptionPane.showMessageDialog(infoParentTab, "Please, always hit the Enter button to effect your changes on the table", "USER ERROR!", ERROR_MESSAGE);

 }

 } else {
    JOptionPane.showMessageDialog(infoParentTab, "You must select a Staff from the Browser...", "USER ERROR!", ERROR_MESSAGE);
 }
 } catch (SQLException e) {
    JOptionPane.showMessageDialog(null, e);
    e.printStackTrace();
 }
试试看{
//打开连接。。。
conn=javaConnect.ConnectDb();
//选择所选职员的资格表行
String sql2=“从QualificationTable中选择*,其中qualID=“+theRowID;
pStmt=连接准备状态(sql2);
ResultSet rs2=pStmt.executeQuery();
//检查QualificationTable在该行上是否有内容。。。
if(rs2.next()){
//它有内容更新。。。
//获取qual表格的模型。。。
DefaultTableModel tModel=(DefaultTableModel)qualTable.getModel();
对于(int i=0;i
我实际上想做的是使用一个链接到数据库的表来存储公司员工的资格。现在,资格数据库中的每个条目都通过qualID引用到员工数据库中的员工

因此,当我将资格证书存储在表中时,它也会记录具有资格证书的员工。这将使我能够在需要时从数据库中检索特定员工的资格

如果为空,则插入数据库的段工作正常(即else…段)。但是更新段(即if…段)是错误的,因为代码使用JTable上的最后一行填充数据库表中的所有行,而不是在需要更新时将所有新更改复制到数据库表中


我已尽了我所能,但毫无结果。拜托,我需要很多帮助…时间不在我这边。tnx guys Prevance

最好的方法是使用
CachedRowSet
备份
JTable的
模型。您将能够轻松查看、插入和更新数据


以下是教程:

请告诉我,我真的不知道如何实现这个CachedRowSet…这只是我代码的一部分…我已经用普通的resultset方法完成了大量的数据库工作…我希望这并不意味着我必须重新开始?你可以做任何你想做的事情。我刚刚给了你一个教程的链接,该教程创建了一个
JTable/CachedRowSet
程序来对数据库表执行操作。好的,谢谢兄弟……请不要生气,我只是感到沮丧:(别担心,我没有生气。为你提供一个正确格式的程序的链接比通过你的代码来解决问题更容易。你混合了数据访问代码和GUI,这在任何真正的程序中都是一个巨大的禁忌。kayaman如果我打扰了你,请道歉……我花时间阅读了教程,我发现我是真实的我需要重新设计很多我没有时间做的事情:(请你看一下我的代码,特别是if(rs.next())部分。我也不明白你的意思,我混合了数据访问代码和GUI…tnx很多兄弟