简体   繁体   中英

Java SQL query failing to return valid ResultSet

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.

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().

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM