游标状态无效java.sql.SQLException

时间:2015-01-13 18:23:29

标签: java jdbc jdbc-odbc

我创建了一个用于将数据插入MSAccess数据库的程序。当我执行语句时,我得到一个java.sql.SQLException:[Microsoft] [ODBC驱动程序管理器]无效的游标状态。 数据库是空的。我尝试通过执行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.access$1400(LogInInterface.java:12)
at com.company.LogInInterface$9.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.access$400(EventQueue.java:81)
at java.awt.EventQueue$2.run(EventQueue.java:633)
at java.awt.EventQueue$2.run(EventQueue.java:631)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
at java.awt.EventQueue$3.run(EventQueue.java:647)
at java.awt.EventQueue$3.run(EventQueue.java:645)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.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)

1 个答案:

答案 0 :(得分:3)

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

  

使用此ResultSet对象的当前行的新内容更新基础数据库。 当光标位于插入行上时,无法调用此方法。

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

  

将光标移动到插入行。当光标位于插入行上时,将记住当前光标位置。插入行是与可更新结果集关联的特殊行。它本质上是一个缓冲区,其中可以通过在将行插入结果集之前调用updater方法来构造新行。 当光标位于插入行 时,只能调用updater,getter和insertRow方法。在调用insertRow之前,每次调用此方法时,必须为结果集中的所有列赋值。必须先调用updater方法,然后才能在列值上调用getter方法。