1

This is driving me mad because I cannot make any sense of it. I am executing the following code:

nameString = jcbClientList.getItemAt(jcbClientList.getSelectedIndex());
System.out.println(" Name String = " + nameString );
sql = "SELECT * FROM clients WHERE Name = \'" + nameString + "\'";
System.out.println(sql);

try {
    Statement st = con.createStatement();
    ResultSet rs = st.executeQuery(sql);
    while( rs.next()) {
        clientID = rs.getInt(1);
    }
}
catch(SQLException se) {
    msg = "Problem getting client ID from DB \n" + se.getLocalizedMessage();
    System.out.println(msg);
    JOptionPane.showMessageDialog(null, msg);
}

The SQL string be built is correct. I have checked this by taking the System.out.println(sql) output of the string and pasting it into other code and it work perfectly. However, in this context I am getting an exception:

Invalid cursor state - no current row.

Even if I change the sql to be 'SELECT * FROM clients' which should return 20 rows and does elsewhere in the application, it still gives the same error. The database being addressed is an embedded Derby DB.

Steve
  • 1,553
  • 2
  • 20
  • 29
  • Possible duplicate of [Java SQL Exception Invalid Cursor State - no current row](http://stackoverflow.com/questions/16576331/java-sql-exception-invalid-cursor-state-no-current-row) – DimaSan Sep 20 '16 at 18:15
  • 3
    Are you sure that the code you posted is giving that error? Which line exactly is the one producing the error? Maybe you're looking in the wrong place. – sstan Sep 20 '16 at 18:22
  • Are you sure you're connecting to the correct database? – Dave Sep 20 '16 at 18:23
  • Yes, the database is embedded and the connection is set up at the start of the application and used in all instances. This is the only place there is a problem. – NigelGordon Sep 20 '16 at 18:37
  • The error is occurring at the line while( rs.next()) – NigelGordon Sep 20 '16 at 18:38
  • your result set has multiple fields when you do select * , since you are only getting the id why dont you change to select id instead? – logger Sep 20 '16 at 18:47
  • I have yet to get round to writing the code to process the other fields in the dataset. Will do that once I have got this bit working. Will need the whole dataset in the end. – NigelGordon Sep 21 '16 at 08:22
  • Are any of the columns in the clients table a BLOB or CLOB? – Bryan Pendleton Sep 22 '16 at 03:42
  • Bryan, there are no BLOB or CLOB columns. The three columns are: INTEGER, VARCHAR(45), INTEGER. – NigelGordon Sep 22 '16 at 22:01

1 Answers1

0

I seem to recall having run into specific JDBC drivers that did not properly implement the part that says " A ResultSet cursor is initially positioned before the first row ". I got around it by first doing a first() (or beforeFirst()) call and only then start invoking next().

Erwin Smout
  • 18,113
  • 4
  • 33
  • 52
  • I have come across that before and have invoked the statement with the type set to give a scrollable result set, then implemented beforeFirst(). It made no difference. The thing is the same piece of code in another location is working no problems. – NigelGordon Sep 22 '16 at 22:04