游标状态无效 java.sql.SQLException

Invalid cursor state java.sql.SQLException

我创建了一个将数据插入 MSAccess 数据库的程序。当我执行这些语句时,我得到一个 java.sql.SQLException: [Microsoft][ODBC Driver Manager] Invalid cursor state 。 数据库是空的。我试图通过执行 resultSet.next(); 将光标移动到第一行;但它不起作用。谁能告诉我问题是什么??到目前为止,这是我的代码:

创建连接的代码:

    public LogInInterface() {
    initComponents();

      //Database Connection Setup.
      try{
          String Driver = "sun.jdbc.odbc.JdbcOdbcDriver";
          Class.forName(Driver);
          //ErrorLabel.setText("DRIVER LOADED");
          //ErrorLabel.setForeground(new Color(0, 204, 0));
          String Login = "jdbc:odbc:JavaDB";
          connection = DriverManager.getConnection(Login);
          statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
          resultSet = statement.executeQuery(SQL);
          //ErrorLabel.setText("DATABASE CONNECTION IS READY");
          //ErrorLabel.setForeground(new Color(0, 204, 0));
          //ErrorLabel.setText(null);


      }catch(Exception e){
          ErrorLabel.setText("DATABASE CONNECTION ERROR (Code 1)");
          e.printStackTrace();
      }

}

插入数据库的代码:

 private void RegisterButtonActionPerformed(ActionEvent e) {
    // TODO add your code here
    try{
        String FName = FirstNameTextField.getText();
        String LName = LastNameTextField.getText();
        resultSet.moveToInsertRow();
        resultSet.updateString(2,FName );
        resultSet.updateString(3,LName);
        resultSet.updateRow();
        statement.close();
        resultSet.close();
        statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
        resultSet = statement.executeQuery(SQL);

    }catch(Exception e1){
        ErrorLabel.setText("ACCOUNT COULD NOT BE CREATED AT THIS TIME (Code 2)");
        e1.printStackTrace();
    }
}

完整的堆栈跟踪:

java.sql.SQLException: [Microsoft][ODBC Driver Manager] Invalid cursor state
at sun.jdbc.odbc.JdbcOdbcResultSet.setPos(JdbcOdbcResultSet.java:5271)
at sun.jdbc.odbc.JdbcOdbcResultSet.updateRow(JdbcOdbcResultSet.java:4171)
at com.company.LogInInterface.RegisterButtonActionPerformed(LogInInterface.java:251)
at com.company.LogInInterface.access00(LogInInterface.java:12)
at com.company.LogInInterface.actionPerformed(LogInInterface.java:545)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6297)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3275)
at java.awt.Component.processEvent(Component.java:6062)
at java.awt.Container.processEvent(Container.java:2039)
at java.awt.Component.dispatchEventImpl(Component.java:4660)
at java.awt.Container.dispatchEventImpl(Container.java:2097)
at java.awt.Component.dispatchEvent(Component.java:4488)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4575)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4236)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4166)
at java.awt.Container.dispatchEventImpl(Container.java:2083)
at java.awt.Window.dispatchEventImpl(Window.java:2489)
at java.awt.Component.dispatchEvent(Component.java:4488)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:674)
at java.awt.EventQueue.access0(EventQueue.java:81)
at java.awt.EventQueue.run(EventQueue.java:633)
at java.awt.EventQueue.run(EventQueue.java:631)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext.doIntersectionPrivilege(AccessControlContext.java:87)
at java.security.AccessControlContext.doIntersectionPrivilege(AccessControlContext.java:98)
at java.awt.EventQueue.run(EventQueue.java:647)
at java.awt.EventQueue.run(EventQueue.java:645)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:644)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

ResultSet.updateRow 替换为 ResultSet.insertRow。来自 Javadoc of ResultSet.updateRow:

Updates the underlying database with the new contents of the current row of this ResultSet object. This method cannot be called when the cursor is on the insert row.

另请查看您在方法中调用的 ResultSet.moveToInsertRow 的 Javadoc:

Moves the cursor to the insert row. The current cursor position is remembered while the cursor is positioned on the insert row. The insert row is a special row associated with an updatable result set. It is essentially a buffer where a new row may be constructed by calling the updater methods prior to inserting the row into the result set. Only the updater, getter, and insertRow methods may be called when the cursor is on the insert row. All of the columns in a result set must be given a value each time this method is called before calling insertRow. An updater method must be called before a getter method can be called on a column value.