检查Java中的ResultSet是否为空
我正在我的程序中使用HSQLDB。我想检查我的结果集是否为空检查Java中的ResultSet是否为空,java,jdbc,hsqldb,Java,Jdbc,Hsqldb,我正在我的程序中使用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
//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()
和.beforFirst()
。我还尝试添加connection.createStatement(ResultSet.TYPE\u SCROLL\u不敏感,ResultSet.CONCUR\u只读)代码>
但我还是得到了同样的结果(我得到的消息是空的,数据来自数据库!!!)
我做错了什么?如果我理解你的目标,你可以使用边做边循环
if (!results.next()) {
System.out.println("empty");
} else {
//display results
do {
String data = results.getString("first_name");
//name.setText(data);
System.out.println(data);
} while (results.next());
}
或者,你可以像这样保持一个计数
int count = 0;
//display results
while (results.next()) {
String data = results.getString("first_name");
//name.setText(data);
System.out.println(data);
count++;
}
if (count < 1) {
// Didn't even read one row
}
int count=0;
//显示结果
while(results.next()){
字符串数据=results.getString(“first_name”);
//name.setText(数据);
系统输出打印项次(数据);
计数++;
}
如果(计数<1){
//甚至连一行都没读
}
问题出在哪里?观察:第一次调用时,results.next()
将光标移动到第一行(在if
中)。因此,如果有数据,while循环的条件将在它开始有效跳过第一行时将其移动到第二行。您可以使用do while循环。您链接的问题也只有FORWARD\u
resultset的答案(例如:已接受的答案)。