这不是很简单的操作吗?但是,我看到既没有size()
也没有length()
方法。
改为执行SELECT COUNT(*) FROM ...
查询。
OR
int size =0;
if (rs != null)
{
rs.last(); // moves cursor to the last row
size = rs.getRow(); // get row id
}
在任何一种情况下,您都不必遍历整个数据。
double rowCount = ((ResultSetImpl)resultSet).getUpdateCount()
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.
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();
: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
}
嗯,如果您有ResultSet
类型的ResultSet.TYPE_FORWARD_ONLY
,则要保持这种方式(并且not切换到ResultSet.TYPE_SCROLL_INSENSITIVE
或ResultSet.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]
if(rs.next())
System.out.println("Recordcount: "+rs.getInt("RECORDCOUNT"));//hack: first record contains the record count
while(rs.next())
//do your stuff
使用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()
[[速度考虑]
这里很多ppl都建议stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
,但为此您需要打开连接作为ResultSet.last()
,对于Derby嵌入式数据库,此连接的[ResultSet.TYPE_SCROLL_INSENSITIVE
的10倍。
ResultSet.TYPE_FORWARD_ONLY
的速度明显更快。 SELECT COUNT(*)
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(); }
ResultSetI>]接口的运行时值,发现它一直都是ResultSetImpl。 ResultSetImpl有一个称为 此代码示例应满足:rs.beforeFirst();
的方法,该方法返回您要查找的值。getUpdateCount()
ResultSet resultSet = executeQuery(sqlQuery);
我意识到向下转换通常是不安全的过程,但是这种方法还没有使我失败。