【发布时间】:2016-08-12 14:13:53
【问题描述】:
我已经尝试了一段时间了,当我单击更新按钮时,它显示“已成功保存详细信息”并且没有更新。我不知道我做错了什么。
下面是我在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)
-
你有没有调试过这个,如果有,怎么调试?您是否至少打印出要提交给准备好的语句的字符串?如果是这样,您是否看到了您希望看到的字符串?
-
@HovercraftFullOfEels 请告诉我如何调试和打印字符串。我是编码新手。
-
例如,在创建 Cert_Name 变量后立即调用:
System.out.println("cert name: " + Cert_Name);(尽管它应该被称为 certName 以符合 Java 命名标准)。