小编典典
谢谢@Germann …
我已经解决了,我将提供解决方案,以便其他人可以从中获得帮助。
正确,它resultSet.absolute(...);返回布尔值,但是它也将光标移动到其参数中的指定行resultSet.absolute(table.getSelectedRow());。那么问题是什么。
问题是:dTableModel.removeRow(table.getSelectedRow());不能在该行 之前调用,
resultSet.absolute(table.getSelectedRow());因为(第一个)它删除了选定的行,并且因为它被删除了,所以第二个方法没有选择任何内容,因此table.getSelectedRow()返回-1。然后按照文档中的说明absolute(-1)将光标移动到最后一行,这将删除基础数据库中的最后一行。
所以解决方案是颠倒那些行的顺序,我更喜欢在之后 resultSet.deleteRow();
JButton removeEmployee = new JButton("Remove Selected");
removeEmployee.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{/* here I added +1 because it moves the row to the selected row -1
I don't know why. But it now works well */
resultSet.absolute(table.getSelectedRow()+1);
resultSet.deleteRow();
dTableModel.removeRow(table.getSelectedRow());
} catch (SQLException e1)
{
e1.printStackTrace();
}
}
});
2020-11-01