Search code examples
javasqlderbyembedded-database

java.sql.SQLException: Invalid cursor state - no current row


I keep getting this error when I use the getString method from the ResultSet. I use the returned ResultSet of the first method for the second method. Relevant Code:

public ResultSet getStudentRS(){
    try{
        studentRS = s.executeQuery("SELECT * FROM Students "
                + "WHERE StudentID=210569906");
    }
    catch(SQLException error){
        System.err.println("Unable to query for getStudentRS.");
        error.printStackTrace(System.err);
        System.exit(0);
    }
    return studentRS;
}

public String getUserName(){
    try{
        while(rs.next())
            return rs.getString(1) + " " + rs.getString(2); // Exception Here
    }
    catch(SQLException error){
        System.err.println("Unable to query for getUserName.");
        error.printStackTrace(System.err);
        System.exit(0);
    }
    return "Failure";
}

Any help is greatly appreciated in resolving this issue.


Solution

  • There is a semi-colon after your while statement.

    while(rs.next());