Java 从JTable更新MYSQL数据库
我已经尝试了一段时间,现在,当我点击更新按钮,它说'细节保存成功',并没有更新。我不知道我做错了什么 下面是我在Java 从JTable更新MYSQL数据库,java,mysql,swing,Java,Mysql,Swing,我已经尝试了一段时间,现在,当我点击更新按钮,它说'细节保存成功',并没有更新。我不知道我做错了什么 下面是我在JTable上编辑按钮的代码: private void jButtonEditEmployeeActionPerformed(java.awt.event.ActionEvent evt) { try { //int emp_id= jTable1.getSelectedRow(); // String tableClick=(jTa
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");
}
}
private void jbuttonedemployeeActionPerformed(java.awt.event.ActionEvent evt){
试一试{
//int emp_id=jTable1.getSelectedRow();
//字符串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“,”根“,”);
con.setAutoCommit(假);
String sql=“更新证书集证书名称=?,供应商=?,证书代码=?”;
PreparedStatement pstmt=con.prepareStatement(sql);
int rows=jTable1.getRowCount();
对于(int row=0;row
您需要的条件是
"UPDATE certificate SET Cert_Name=?, Vendor=?, Cert_Code=? where..."
但始终存在最后一个值请格式化您的代码以提高可读性。请记住关闭finally Block中的所有资源(连接、PreparedStatements)是否对此进行了调试,如果是,如何调试?您是否已至少打印出提交给准备好的报表的字符串?如果是这样,您是否看到了希望看到的字符串?@hovercraftfullofels请告诉我如何调试和打印字符串。我在编码方面是新手。例如,调用:
System.out.println(“cert name:+cert\u name”)代码>在创建Cert_Name变量之后(尽管为了符合Java命名标准,它应该被称为certName)。仍然什么都不做。您可能有类似的代码示例吗?谢谢