java.sql.SQLException: 无效的游标状态 - 没有当前行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6615248/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.sql.SQLException: Invalid cursor state - no current row
提问by RandellK02
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:
当我使用 ResultSet 中的 getString 方法时,我不断收到此错误。我将第一种方法返回的 ResultSet 用于第二种方法。相关代码:
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.
非常感谢在解决此问题方面的任何帮助。
回答by Jon Skeet
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 offthe 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.
您正在遍历所有数据,直到将光标移出最后一行……然后尝试获取数据。鉴于您的缩进,我怀疑您不打算在 while 循环末尾使用分号 - 这就是为什么使用大括号总是好的原因,并且让您的 IDE 格式化您的代码......它使这种事情很明显。即使没有分号,我也不认为这是表达它的最佳方式。
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?
}
回答by Kal
There is a semi-colon after your while statement.
while 语句后有一个分号。
while(rs.next());