我在我的程序中使用HSQLDB。我想检查我的结果集是否为空。
//check if empty first
if(results.next() == false){
System.out.println("empty");
}
//display results
while (results.next()) {
String data = results.getString("first_name");
//name.setText(data);
System.out.println(data);
}
上述方法无法正常工作。根据这篇帖子,我必须调用
.first()
或.beforeFirst()
将光标重置为第一行,但是在HSQL中不支持.first()
和.beforeFirst()
。我还尝试添加connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
,但结果仍然相同(我收到了来自数据库的空消息和数据!!!)
我在这里做错了什么?
results.next()
在第一次调用时(在if
中)将光标移动到第一行。因此,如果有数据,您的 while 循环条件将在开始时将其移动到第二行,从而有效地跳过第一行。您可以改用 do-while 循环。 - IslayFORWARD_ONLY
结果集的答案(例如:被接受的答案)。 - Mark Rotteveel