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

这条路对吗

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

当前回答

你通常会这样做:

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

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

其他回答

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

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

为什么不使用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)”似乎工作得很好。

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

        if (res.next()) {
            do {

                // successfully in. do the right things.

            } while (res.next());
        } else {
           // no results back. warn the user.
        }

你总是可以在前面做下一个,只做一个后循环检查

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

    do {
     //statement(s)
    } while (resultSet.next());
}

你通常会这样做:

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

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