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

这条路对吗

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

当前回答

if(resultSet.first) {

} else { 
    system.out.println("No raw or resultSet is empty");
}

因为如果ResultSet没有raw,那么ResultSet。第一个返回false。

其他回答

你通常会这样做:

while ( resultSet.next() ) { 
   // Read the next item
   resultSet.getString("columnName");
}

如果要报告一个空集,请添加一个变量来计算读取的项。如果您只需要阅读一个项目,那么您的代码就足够了。

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

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

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

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

提醒一句,

如果你使用的是org.springframework.jdbc.core.RowMapper 非常小心你是如何检查你的ResultSet是空的,因为 下面的语句适用于mapRow方法:

该方法不应该在ResultSet上调用next();它只应该映射当前行的值。<-来自文档

在这种情况下,你的支票可能会变成

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

为什么不使用rs.getRow()?

int getRow()
           throws SQLException
Retrieves the current row number. The first row is number 1, the second number 2, and so on.
Note:Support for the getRow method is optional for ResultSets with a result set type of TYPE_FORWARD_ONLY

Returns:
the current row number; 0 if there is no current row
Throws:
SQLException - if a database access error occurs or this method is called on a closed result set
SQLFeatureNotSupportedException - if the JDBC driver does not support this method
Since:
1.2

对我来说,检查“if (rs.getRow() != 0)”似乎工作得很好。