开发者

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.


There is a semi-colon after your while statement.

while(rs.next());


Look at this code:

while(rs.next());
return rs.getString(1) + " " + rs.getString(2);

You're looping through all the data until you've moved the cursor off the last line... and then tried to get data. Given your indentation, I suspect you didn't intend the semi-colon at the end of the while loop - that's why it's always good to use braces, and also to get your IDE to format your code... it makes this sort of thing obvious. Even without the semi-colon though, I don't think it's the best way of expressing it.

I think you want:

if (rs.next())
{
    return rs.getString(1) + " " + rs.getString(2);
}
else
{
    // What do you want to do if there are no results?
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜