Reputation: 83
I created a program for inserting data into MSAccess Database. When i execute the statements I get an java.sql.SQLException: [Microsoft][ODBC Driver Manager] Invalid cursor state . The database is empty. I tried to move the cursor to the first row by executing resultSet.next(); but it doesn't work. Can anyone tell me what the problem is?? This is my code so far:
Code for creating the connection:
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();
}
}
Code for inserting into database:
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();
}
}
Complete stacktrace:
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)
Upvotes: 1
Views: 2824
Reputation: 72844
Replace ResultSet.updateRow
with ResultSet.insertRow
. From the 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.
Also look at the Javadoc of ResultSet.moveToInsertRow
which you're calling in the method:
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.
Upvotes: 3