如何查找 Java 中的结果集是否为空?

2022-09-01 06:25:00

如何找到通过查询数据库获得的 ,是否为空?ResultSet


答案 1

在执行语句之后,您可以立即拥有一个 if 语句。例如

ResultSet rs = statement.execute();
if (!rs.next()){
//ResultSet is empty
}

答案 2

这绝对给出了很好的解决方案,

ResultSet rs = stmt.execute("SQL QUERY");
// With the above statement you will not have a null ResultSet 'rs'.
// In case, if any exception occurs then next line of code won't execute.
// So, no problem if I won't check rs as null.

if (rs.next()) {
    do {
      // Logic to retrieve the data from the resultset.
      // eg: rs.getString("abc");
    } while(rs.next());
} else {
    // No data
}

推荐