我知道要删除一行我应该这样做:
if (table.getSelectedRow() > -1) {
int rowToTable = table.getSelectedRow();
int rowToModel = table.convertRowIndexToModel(rowToTable);
model.removeBook(rowToModel);
} else {
JOptionPane.showMessageDialog(null, "Select A Row");
}
但是,现在我尝试这种没有rowToModel
变量的方法,仍然可以正确删除:
if (table.getSelectedRow() > -1) {
int rowToTable = table.getSelectedRow();
model.removeBook(rowToTable);
} else {
JOptionPane.showMessageDialog(null, "Select A Row");
}
我的removeBook()
方法:
public void removeBook(int row) {
Connection connection;
PreparedStatement preparedStatement;
String query = "Delete from BookTable where id=?";
try {
connection = DriverManager.getConnection(...);
Object id = this.getValueAt(row, 0);
preparedStatement = connection.prepareStatement(query);
preparedStatement.setObject(1, id);
if (preparedStatement.executeUpdate() == 1) {
this.removeRow(row);
}
} catch (SQLException sqle) {
sqle.printStackTrace();
}
}
为什么这两个都有效?!哪个是正确的?