这不是很简单的操作吗?但是,我看到既没有size()
nor length()
方法也没有方法。
这不是很简单的操作吗?但是,我看到既没有size()
nor length()
方法也没有方法。
Answers:
做一个SELECT COUNT(*) FROM ...
查询,而不是。
要么
int size =0;
if (rs != null)
{
rs.last(); // moves cursor to the last row
size = rs.getRow(); // get row id
}
无论哪种情况,您都不必遍历整个数据。
select count
?
ResultSet#last()
不适用于所有类型的ResultSet
对象,您需要确保您使用的是ResultSet.TYPE_SCROLL_INSENSITIVE
ResultSet.TYPE_SCROLL_SENSITIVE
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
}
ps=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
好吧,如果您有一个ResultSet
类型ResultSet.TYPE_FORWARD_ONLY
,则要保持这种方式(而不是切换到a 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
ResultSet.TYPE_FORWARD_ONLY
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()
解决方案是:
stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
ResultSet.TYPE_FORWARD_ONLY
到ResultSet.TYPE_SCROLL_INSENSITIVE
通常会导致巨大的性能损失。
SELECT COUNT(*) FROM default_tbl
之前时,SELECT COUNT(*) FROM default_tbl
总共花费不到1.5秒。我在嵌入式derby数据库10.11.1.1上进行了测试
[速度考虑]
这里有很多ppl建议,ResultSet.last()
但是为此,您需要打开连接,ResultSet.TYPE_SCROLL_INSENSITIVE
因为对于Derby嵌入式数据库,该连接的速度比慢 10倍ResultSet.TYPE_FORWARD_ONLY
。
根据我对嵌入式Derby和H2数据库的微型测试,SELECT COUNT(*)
在进行SELECT之前调用起来明显更快。
这是进行行计数的简单方法。
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
String sql = "select count(*) from message";
ps = cn.prepareStatement(sql);
rs = ps.executeQuery();
int rowCount = 0;
while(rs.next()) {
rowCount = Integer.parseInt(rs.getString("count(*)"));
System.out.println(Integer.parseInt(rs.getString("count(*)")));
}
System.out.println("Count : " + rowCount);
我检查了ResultSet接口的运行时值,发现它一直都是ResultSetImpl。ResultSetImpl有一个称为的方法getUpdateCount()
,该方法返回您要查找的值。
此代码示例应满足:
ResultSet resultSet = executeQuery(sqlQuery);
double rowCount = ((ResultSetImpl)resultSet).getUpdateCount()
我意识到向下转换通常是不安全的过程,但是这种方法尚未使我失败。
java.lang.ClassCastException: org.apache.tomcat.dbcp.dbcp.DelegatingResultSet cannot be cast to com.mysql.jdbc.ResultSetImpl
今天,我使用这种逻辑为什么不知道获得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.
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.first()
在执行解决之后以这种方式使用:
if(rs.first()){
// Do your job
} else {
// No rows take some actions
}
文档(链接):
boolean first() throws SQLException
将光标移动到该
ResultSet
对象的第一行。返回值:
true
如果光标在有效行上;false
如果结果集中没有行抛出:
SQLException
-如果发生数据库访问错误;在封闭的结果集上调用此方法,或者结果集类型为TYPE_FORWARD_ONLY
SQLFeatureNotSupportedException
-如果JDBC驱动程序不支持此方法以来:
1.2
给列命名。
String query = "SELECT COUNT(*) as count FROM
将ResultSet对象中的该列引用为int并从那里进行逻辑处理。
PreparedStatement statement = connection.prepareStatement(query);
statement.setString(1, item.getProductId());
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
int count = resultSet.getInt("count");
if (count >= 1) {
System.out.println("Product ID already exists.");
} else {
System.out.println("New Product ID.");
}
}