如何获取 java.sql.ResultSet 的大小?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5131331/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 09:36:29  来源:igfitidea点击:

How to get the size of a java.sql.ResultSet?

javajdbc

提问by user225269

I want to get the size of the ResultSetinside the while loop.

我想获得ResultSetwhile 循环内部的大小。

Tried the code below, and I got the results that I want. But it seems to be messing up with result.next()and the while loop only loops once if I do this. What's the proper way of doing this?

尝试了下面的代码,我得到了我想要的结果。但它似乎搞砸了,result.next()如果我这样做,while 循环只会循环一次。这样做的正确方法是什么?

result.first();
while (result.next()){

    System.out.println(result.getString(2));
    System.out.println("A. " + result.getString(5) + "\n" + "B. " + result.getString(6) + "\n" + "C. " + result.getString(7) + "\n" + "D. " + result.getString(8));
    System.out.println("Answer: ");
    answer = inputquiz.next();

    result.last();

    if (answer.equals(result.getString(10))) {
        score++;
        System.out.println(score + "/" + result.getRow());
    } else {
        System.out.println(score + "/" + result.getRow());
    }
}

回答by BalusC

What's the proper way of doing this?

这样做的正确方法是什么?

Map it to a List<Entity>. Since your code is far from self-documenting (you're using indexes instead of column names), I can't give a well suited example. So I'll take a Personas example.

将其映射到List<Entity>. 由于您的代码远非自我记录(您使用的是索引而不是列名),因此我无法给出一个非常合适的示例。所以我会以一个Person为例。

First create a javabean class representing whatever a single row contains.

首先创建一个 javabean 类来表示单行包含的任何内容。

public class Person {
    private Long id;
    private String firstName;
    private String lastName;
    private Date dateOfBirth;

    // Add/generate c'tors/getters/setters/equals/hashcode and other boilerplate.
}

(a bit decent IDE like Eclipse can autogenerate them)

(像 Eclipse 这样有点像样的 IDE 可以自动生成它们)

Then let JDBC do the following job.

然后让JDBC做下面的工作。

List<Person> persons = new ArrayList<Person>();

while (resultSet.next()) {
    Person person = new Person();
    person.setId(resultSet.getLong("id"));
    person.setFirstName(resultSet.getString("fistName"));
    person.setLastName(resultSet.getString("lastName"));
    person.setDataOfBirth(resultSet.getDate("dateOfBirth"));
    persons.add(person);
}

// Close resultSet/statement/connection in finally block.

return persons;

Then you can just do

然后你可以做

int size = persons.size();

And then to substitute your code example

然后替换您的代码示例

for (int i = 0; i < persons.size(); i++) {
    Person person = persons.get(i);
    System.out.println(person.getFirstName());
    int size = persons.size(); // Do with it whatever you want.
}

See also:

也可以看看:

回答by Bala R

you could do result.last();and call result.getRow();(which retrieves the current row number) to get count. but it'll have load the all the rows and if it's a big result set, it might not be very efficient. The best way to go about is to do a SELECT COUNT(*)on you query and get the count like it's demonstrated in this post, beforehand.

您可以执行result.last();并调用result.getRow();(检索当前行号)以获取计数。但它会加载所有行,如果它是一个很大的结果集,它可能不是很有效。最好的方法是事先SELECT COUNT(*)对您进行查询并获取计数,就像这篇文章中展示的那样。

回答by Stephen C

This is a tricky question.

这是一个棘手的问题。

  1. Normally, result.last()scrolls to the end of the ResultSet, and you can't go back.

  2. If you created the statement using one of the createStatementor prepareStatementmethods with a "resultSetType" parameter, and you've set the parameter to ResultSet.TYPE_SCROLL_INSENSITIVEor ResultSet.TYPE_SCROLL_SENSITIVE, then you can scroll the ResultSetusing first()or relative()or some other methods.

  1. 通常,result.last()滚动到 的末尾ResultSet,并且无法返回。

  2. 如果您使用带有“resultSetType”参数的createStatementorprepareStatement方法之一创建了语句,并且您已将该参数设置为ResultSet.TYPE_SCROLL_INSENSITIVEor ResultSet.TYPE_SCROLL_SENSITIVE,那么您可以滚动ResultSetusing first()orrelative()或其他一些方法。

However, I'm not sure if all databases / JDBC drivers support scrollable result sets, and there are likely to be performance implications in doing this. (A scrollable result set implies that either the database or the JVM needs to buffer the entire resultset somewhere ... or recalculate it ... and that's expensive for a large resultset.)

但是,我不确定是否所有数据库/JDBC 驱动程序都支持可滚动的结果集,并且这样做可能会对性能产生影响。(可滚动的结果集意味着数据库或 JVM 需要在某处缓冲整个结果集......或重新计算......这对于大型结果集来说很昂贵。)

回答by Anptk

The way of getting size of ResultSet, No need of using ArrayList etc

ResultSet大小的获取方式,无需使用ArrayList等

int size =0;  
if (rs != null)   
{  
rs.beforeFirst();  
 rs.last();  
size = rs.getRow();
}

Now You will get size, And if you want print the ResultSet, before printing use following line of code too,

现在你会得到大小,如果你想打印结果集,在打印之前也使用以下代码行,

rs.beforeFirst();  

回答by Dead Programmer

There are also another way to get the count from DB.

还有另一种方法可以从 DB 获取计数。

Note : This column gets updated when DBA'S do realtime statistics

注意:当 DBA 进行实时统计时,此列会更新

select num_rows from all_Tables where table_name ='<TABLE_NAME>';