【发布时间】:2016-02-21 03:40:18
【问题描述】:
Java 代码
int dialogDelete=JOptionPane.showConfirmDialog(null,"Are you sure you want to delete this Personnel?", "DELETE PERSONNEL",JOptionPane.YES_NO_OPTION);
if(dialogDelete==JOptionPane.YES_OPTION){
DefaultTableModel model = (DefaultTableModel)tbl_personnel.getModel();
int row = tbl_personnel.getSelectedRow();
String url = "jdbc:mysql://localhost/mypos";
String username = "root";
String password = "";
try {
Connection connection = DriverManager.getConnection(url,username,password);
System.out.println("database connected");
stmt = connection.createStatement();
stmt.execute("DELETE FROM Personnel WHERE Id ="+row+"");
} catch(Exception e) {
throw new IllegalStateException("cannot",e);
}
int modelRow = tbl_personnel.convertRowIndexToModel( row );
model.removeRow( modelRow );
}
我已经编辑了代码。我现在可以删除 JTable 中的行并删除 mysql 中的行,但是 JTable 中删除的行不是 mysql 中删除的行。我不确定出了什么问题。
【问题讨论】:
-
这将取决于 TableModel