结果集没有hasNext方法。我想检查resultSet是否有任何值

这条路对吗

if (!resultSet.next() ) {
    System.out.println("no data");
} 

当前回答

if (!resultSet.isAfterLast() ) {    
System.out.println("No data"); 
} 

isAfterLast()对于空结果集也返回false,但由于游标无论如何都在第一行之前,这个方法似乎更清楚。

其他回答

我相信这是一篇实用且易于阅读的文章。

        if (res.next()) {
            do {

                // successfully in. do the right things.

            } while (res.next());
        } else {
           // no results back. warn the user.
        }
if (resultSet==null ) {
    System.out.println("no data");
}

我认为检查结果集最简单的方法是通过包org.apache.commons.collections.CollectionUtils下的CollectionUtils

if(CollectionUtils.isNotEmpty(resultList)){
  /**
  * do some stuff
  */
}

这将检查null以及空结果集条件。

有关更详细的信息,您可以参考下面的文档。 CollectionUtils

ResultSet rs = rs.executeQuery();
if(rs.next())
{
  rs = rs.executeQuery();
  while(rs.next())
  {
    //do code part
  }
}
else
{
  //else if no result set
}

最好重新执行查询,因为当我们调用if(rs.next()){....} ResultSet的第一行将被执行,之后在while(rs.next()){....}我们将从下一行得到结果。因此,我认为在if内部重新执行查询是更好的选择。

最好使用ResultSet.next()和do{…} while()语法。

“检查任何结果”调用ResultSet.next()将光标移动到第一行,因此使用do{…} while()语法来处理该行,同时继续处理循环返回的其余行。

通过这种方式,您可以检查任何结果,同时也可以处理返回的任何结果。

if(resultSet.next()) { // Checks for any results and moves cursor to first row,
    do { // Use 'do...while' to process the first row, while continuing to process remaining rows

    } while (resultSet.next());
}