【发布时间】:2022-01-14 13:54:18
【问题描述】:
求助,我无法删除表格中的最后一行。当我删除其他记录时没有错误,但最后一条,我无法删除它
这是我的代码:
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
int row = table1.getSelectedRow();
if(row >= 0){
int opt = JOptionPane.showConfirmDialog(null, "Confirm to Delete?", "Delete", JOptionPane.YES_NO_OPTION);
if(opt==0){
try{
Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3307/finals?zeroDateTimeBehavior=CONVERT_TO_NULL", "root", "");
DefaultTableModel model = (DefaultTableModel) table1.getModel();
model.removeRow(row);
String value = (table1.getModel().getValueAt(row, 0).toString());
String query = "DELETE FROM student WHERE id_num = '"+ value + "'";
PreparedStatement pst = conn.prepareStatement(query);
pst.executeUpdate(query);
JOptionPane.showMessageDialog(this, "Deleted Successfully. \nYour record has been updated.", "Success", JOptionPane.INFORMATION_MESSAGE);
}
catch(Exception ex){ex.printStackTrace();}
}
}
else{JOptionPane.showMessageDialog(this, "Please select a record.", "Error", JOptionPane.INFORMATION_MESSAGE);}
}
【问题讨论】: