简体   繁体   中英

Updating database from a dynamic jtable

I am trying to update a database from a dynamic JTable. Here is my code

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();
 }

what i am actually trying to do is to use a table linked to a database to store qualifications of staff in a company. now each entry in the qualifications database is referenced to the staffID in the staffs database through qualID.

so when i store the qualification on the table, it also records the staff that has the qualification. this should enable me retrieve a particular staff's qualifications from the database when need.

the segment for inserting into the database if empty works fine (ie the else... segment). but the update segment (ie the if... segment) is faulty in the sense that the code uses the last row on the JTable to populate all the rows in the database table instead of replicating all the new changes into the database table when update is need.

i have tried everything i could to no avail. please i need much help in this...time is not on my side. tnx guys in advance

The best way to do this is to use a CachedRowSet to back up the JTable's model. You'll be able to view, insert and update data easily.

Here's the tutorial: Using JDBC with GUI API

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM