我如何获得java.sql.ResultSet的大小?

问题描述 投票:270回答:15

这不是很简单的操作吗?但是,我看到既没有size()也没有length()方法。

java sql jdbc resultset record-count
15个回答
257
投票

改为执行SELECT COUNT(*) FROM ...查询。

OR

int size =0;
if (rs != null) 
{
  rs.last();    // moves cursor to the last row
  size = rs.getRow(); // get row id 
}

在任何一种情况下,您都不必遍历整个数据。


1
投票
double rowCount = ((ResultSetImpl)resultSet).getUpdateCount()

1
投票
今天,我使用这种逻辑为什么不知道获得RS的数量。

0
投票
int chkSize = 0; if (rs.next()) { do { ..... blah blah enter code here for each rs. chkSize++; } while (rs.next()); } else { enter code here for rs size = 0 } // good luck to u.

0
投票
我遇到了同样的问题。在执行完成后以这种方式使用theStatement=theConnection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); ResultSet theResult=theStatement.executeQuery(query); //Get the size of the data returned theResult.last(); int size = theResult.getRow() * theResult.getMetaData().getColumnCount(); theResult.beforeFirst();

0
投票
最简单的方法,运行Count(*)查询,执行resultSet.next()指向第一行,然后只执行resultSet.getString(1)即可获得计数。代码:

-1
投票
给列一个名称..

87
投票
ResultSet rs = ps.executeQuery();
int rowcount = 0;
if (rs.last()) {
  rowcount = rs.getRow();
  rs.beforeFirst(); // not rs.first() because the rs.next() below will move on, missing the first element
}
while (rs.next()) {
  // do your standard per row stuff
}

19
投票

嗯,如果您有ResultSet类型的ResultSet.TYPE_FORWARD_ONLY,则要保持这种方式(并且not切换到ResultSet.TYPE_SCROLL_INSENSITIVEResultSet.TYPE_SCROLL_INSENSITIVE,以便能够使用[C0 ])。

我建议一个非常好用和有效的技巧,在顶部添加第一行伪造/伪造行,其中包含行数。

实施例

假设您的查询如下

.last()

您的输出看起来像

select MYBOOL,MYINT,MYCHAR,MYSMALLINT,MYVARCHAR
from MYTABLE
where ...blahblah...

只需将代码重构为类似这样的内容:

true    65537 "Hey" -32768 "The quick brown fox"
false  123456 "Sup"    300 "The lazy dog"
false -123123 "Yo"       0 "Go ahead and jump"
false       3 "EVH"    456 "Might as well jump"
...
[1000 total rows]

您的查询输出现在将类似于

Statement s=myConnection.createStatement(ResultSet.TYPE_FORWARD_ONLY,
                                         ResultSet.CONCUR_READ_ONLY);
String from_where="FROM myTable WHERE ...blahblah... ";
//h4x
ResultSet rs=s.executeQuery("select count(*)as RECORDCOUNT,"
                           +       "cast(null as boolean)as MYBOOL,"
                           +       "cast(null as int)as MYINT,"
                           +       "cast(null as char(1))as MYCHAR,"
                           +       "cast(null as smallint)as MYSMALLINT,"
                           +       "cast(null as varchar(1))as MYVARCHAR "
                           +from_where
                           +"UNION ALL "//the "ALL" part prevents internal re-sorting to prevent duplicates (and we do not want that)
                           +"select cast(null as int)as RECORDCOUNT,"
                           +       "MYBOOL,MYINT,MYCHAR,MYSMALLINT,MYVARCHAR "
                           +from_where);

所以您只需要

1000 null     null null    null null
null true    65537 "Hey" -32768 "The quick brown fox"
null false  123456 "Sup"    300 "The lazy dog"
null false -123123 "Yo"       0 "Go ahead and jump"
null false       3 "EVH"    456 "Might as well jump"
...
[1001 total rows]

11
投票
if(rs.next())
    System.out.println("Recordcount: "+rs.getInt("RECORDCOUNT"));//hack: first record contains the record count
while(rs.next())
    //do your stuff

10
投票

使用int i = 0; while(rs.next()) { i++; } 时出现异常

rs.last()

if(rs.last()){
    rowCount = rs.getRow(); 
    rs.beforeFirst();
}

默认情况下是java.sql.SQLException: Invalid operation for forward only resultset ,这意味着您只能使用ResultSet.TYPE_FORWARD_ONLY

解决方案是:]

rs.next()

4
投票

[[速度考虑]

这里很多ppl都建议stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); ,但为此您需要打开连接作为ResultSet.last(),对于Derby嵌入式数据库,此连接的[最多是ResultSet.TYPE_SCROLL_INSENSITIVE的10倍。

根据我对嵌入式Derby和H2数据库的微测试,在进行SELECT之前调用ResultSet.TYPE_FORWARD_ONLY的速度明显更快。

SELECT COUNT(*)


3
投票
这是进行行计数的简单方法。

Here is in more detail my code and my benchmarks


3
投票
获得ResultSet大小的方法,无需使用ArrayList等

ResultSet rs = job.getSearchedResult(stmt); int rsCount = 0; //but notice that you'll only get correct ResultSet size after end of the while loop while(rs.next()) { //do your other per row stuff rsCount = rsCount + 1; }//end while

现在,您将获得大小,并且,如果您要在打印之前也使用以下代码行打印ResultSet,

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


1
投票
我检查了

ResultSetI>]接口的运行时值,发现它一直都是ResultSetImpl。 ResultSetImpl有一个称为rs.beforeFirst(); 的方法,该方法返回您要查找的值。

此代码示例应满足:

getUpdateCount()ResultSet resultSet = executeQuery(sqlQuery);我意识到向下转换通常是不安全的过程,但是这种方法还没有使我失败。

© www.soinside.com 2019 - 2024. All rights reserved.