这不是一个很简单的手术吗?但是,我看到既没有size()也没有length()方法。
当前回答
如果你有一个ResultSet类型的ResultSet。TYPE_FORWARD_ONLY您希望保持这种方式(而不是切换到ResultSet)。TYPE_SCROLL_INSENSITIVE或ResultSet。TYPE_SCROLL_INSENSITIVE,以便能够使用.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
其他回答
当使用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()
解决方案是:
stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
我检查了ResultSet接口的运行时值,发现它几乎一直都是一个ResultSetImpl。ResultSetImpl有一个名为getUpdateCount()的方法,它返回您正在寻找的值。
下面的代码示例应该足够了: ResultSet ResultSet = executeQuery(sqlQuery); double rowCount = ((ResultSetImpl)resultSet).getUpdateCount()
我知道向下转换通常是一个不安全的过程,但这个方法还没有让我失败。
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您希望保持这种方式(而不是切换到ResultSet)。TYPE_SCROLL_INSENSITIVE或ResultSet。TYPE_SCROLL_INSENSITIVE,以便能够使用.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
今天,我用了这个逻辑,为什么我不知道得到RS的计数。
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.
推荐文章
- django test app error -在创建测试数据库时出现错误:创建数据库的权限被拒绝
- 如何使用Java中的Scanner类从控制台读取输入?
- 在Android SQLite中处理日期的最佳方法
- 如何在MySQL表中移动列?
- 如何添加JTable在JPanel与空布局?
- Statement和PreparedStatement的区别
- 在SQL Server中查找重复的行
- 为什么不能在Java中扩展注释?
- 从DateTime中提取小时(SQL Server 2005)
- 在Java中使用UUID的最重要位的碰撞可能性
- 转换列表的最佳方法:map还是foreach?
- 如何分割逗号分隔的字符串?
- Java字符串—查看字符串是否只包含数字而不包含字母
- Mockito.any()传递带有泛型的接口
- 在IntelliJ 10.5中运行测试时,出现“NoSuchMethodError: org.hamcrest. matcher . descripbemismatch”