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

这条路对吗

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

当前回答

最好使用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());
}

其他回答

ResultSet result = stmt.executeQuery(sqlQuery);
if (!result.next())
    status = "ERROR";
else
    status = "SUCCESS";

你通常会这样做:

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

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

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

        if (res.next()) {
            do {

                // successfully in. do the right things.

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

最初,结果集对象(rs)指向BFR(在第一条记录之前)。使用rs.next()后,光标指向第一条记录,rs值为“true”。使用while循环可以打印表中的所有记录。在检索到所有记录后,光标移动到ALR(在最后一条记录之后),它将被设置为null。让我们假设表中有2条记录。

if(rs.next()==false){
    // there are no records found
    }    

while (rs.next()==true){
    // print all the records of the table
    }

简而言之,我们也可以将条件写成while (rs.next())。

你可以这样做

boolean found = false;

while ( resultSet.next() )
{
    found = true;
    resultSet.getString("column_name");
}

if (!found)
    System.out.println("No Data");