获取Java中ResultSet返回的行数 [英] Get Number of Rows returned by ResultSet in Java

查看:102
本文介绍了获取Java中ResultSet返回的行数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了返回特定行数的 ResultSet 。我的代码是这样的:

I have used a ResultSet that returns certain number of rows. My code is something like this:

ResultSet res = getData();
if(!res.next())
{
    System.out.println("No Data Found");
}
while(res.next())
{
    // code to display the data in the table.
}

是否有任何方法可以检查<$ c返回的行数$ C>结果集?或者我必须自己编写?

Is there any method to check the number of rows returned by the ResultSet? Or do I have to write my own?

推荐答案

你可以使用做...而在循环时循环而不是,以便在之后调用 rs.next()循环执行,如下所示:

You could use a do ... while loop instead of a while loop, so that rs.next() is called after the loop is executed, like this:

if (!rs.next()) {                            //if rs.next() returns false
                                             //then there are no rows.
    System.out.println("No records found");

}
else {
    do {
        // Get data from the current row and use it
    } while (rs.next());
}

或者在获取行时自行计算行数:

Or count the rows yourself as you're getting them:

int count = 0;

while (rs.next()) {
    ++count;
    // Get data from the current row and use it
}

if (count == 0) {
    System.out.println("No records found");
}

这篇关于获取Java中ResultSet返回的行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆