检查Java中的ResultSet是否为空[重复]

2023-11-24

我在我的程序中使用 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);
}

上述方法不能正常工作。根据这个post我得打电话.first() or .beforeFirst()将光标停留在第一行,但是.first() and .beforFirst()HSQL 不支持。我也尝试添加connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);但我仍然得到相同的结果(我得到的消息为空,数据来自数据库!!!) 我在这里做错了什么?


如果我理解你的目标,你可以使用do while loop

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());
}

或者,你可以只保留一个count像这样,

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
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

检查Java中的ResultSet是否为空[重复] 的相关文章

随机推荐