Java:删除选定的行ResultSet,从数据库中删除最后一行? [英] Java: Delete a selected row ResultSet, Deletes last row from the database instead?

查看:427
本文介绍了Java:删除选定的行ResultSet,从数据库中删除最后一行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我选择一行并按下删除按钮时:

when I select a row and press the "remove" button:

在swing界面中,删除所选行(如预期的那样)。但

In the swing interface the selected row is removed (as expected). But

在实际数据库中,无论选择的行是什么(不是预期的),都会删除最后一行。
删除的行始终是数据库中的最后一行,无论实际选择的行是什么。我的代码中没有错误也没有抛出异常。它没有任何中断。

In the actual database the last row is deleted whatever the selected row was (not expected). The deleted row is always the last row in the database, whatever the actual selected row was. There are no errors and no exceptions thrown in my code. it works without any interruptions.

我实际上在代码中添加了必要的东西:

I actually added the necessary things to my code:

  Statement sqlStatement = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);

并添加删除按钮以删除所选行:

and Add a "remove" button to delete a selected row:

JButton removeEmployee = new JButton("Remove Selected");
removeEmployee.addActionListener(new ActionListener()
{
  public void actionPerformed(ActionEvent e)
  {
    dTableModel.removeRow(table.getSelectedRow());
    try
    {
      resultSet.absolute(table.getSelectedRow());
      resultSet.deleteRow();

    } catch (SQLException e1)
    {
      e1.printStackTrace();
    }
  }
});


推荐答案

谢谢@Germann ......

Thank you @Germann ...

我解决了,我会提供解决方案,以便其他人可以从中获得帮助。

I solved it, and I will provide the solution so that others can get help from it.

你是对的 resultSet.absolute(...); 返回布尔值,但它也将光标移动到其参数 resultSet.absolute(table。 getSelectedRow()); 。那么问题是什么。

You are right that resultSet.absolute(...); returns boolean value, but it also moves the cursor to the specified row in its argument resultSet.absolute(table.getSelectedRow());. So what was the problem.

问题是:
dTableModel.removeRow(table.getSelectedRow()); 之前不能调用 resultSet.absolute(table.getSelectedRow()); 因为(第一个)它删除了所选行,因为它是删除后,第二个方法没有选中任何内容,因此 table.getSelectedRow()返回-1。并且如文档中所指定的, absolute(-1)将光标移动到最后一行,并删除基础数据库中的最后一行。

The problem is: The line dTableModel.removeRow(table.getSelectedRow()); mustn't be called before resultSet.absolute(table.getSelectedRow()); because (the first) it removes the selected row, and because it is removed, the second method gets nothing selected, so table.getSelectedRow() returns -1. and as specified in the documentation, absolute(-1) moves the cursor to the last line, and that deletes the last row in the underlying database.

因此解决方案是颠倒这些行的顺序,我更喜欢在 resultSet.deleteRow();

So the solution is to reverse the order of those lines, and I prefer to make it after 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();
        }
      }
    });

这篇关于Java:删除选定的行ResultSet,从数据库中删除最后一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆