-1
私はこれをしばらく試しています。更新ボタンをクリックすると、「詳細が正しく保存されました」と表示され、何も更新されません。私は何が間違っているのか分からない。以下 MYTデータベースをJTableから更新する
はJTable
上の編集ボタンのための私のコードです:あなたが条件必要
private void jButtonEditEmployeeActionPerformed(java.awt.event.ActionEvent evt) {
try {
//int emp_id= jTable1.getSelectedRow();
// String tableClick=(jTable1.getModel().getValueAt(emp_id,2).toString());
//Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/employee_certificate", "root", "");
con.setAutoCommit(false);
String sql = "UPDATE certificate SET Cert_Name=?, Vendor=?, Cert_Code=? ";
PreparedStatement pstmt = con.prepareStatement(sql);
int rows = jTable1.getRowCount();
for (int row = 0; row < rows; row++) {
String Cert_Name = (String) jTable1.getValueAt(row, 4);
String Vendor = (String) jTable1.getValueAt(row, 5);
String Cert_Code = (String) jTable1.getValueAt(row, 6);
pstmt.setString(1, Cert_Name);
pstmt.setString(2, Vendor);
pstmt.setString(3, Cert_Code);
//pstmt.setString(4, tableClick);
pstmt.addBatch();
JOptionPane.showMessageDialog(null, "Details edited successfully");
}
pstmt.executeBatch();
con.commit();
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Details not edited");
}
}
がためにあなたのコードをフォーマットしてください最後の値が常にあります読みやすくなります。 –
finallyブロック内のすべてのリソース(Connections、PreparedStatements)を閉じることを忘れないでください。 – SameeraB
これをデバッグしましたか?準備された声明に提出された弦を少なくとも印刷しましたか?もしそうなら、あなたが見たい文字列を見ていますか? –