这不应该是一个非常简单的操作吗?但是,我看到既size()
没有方法也没有length()
方法。
15 回答
SELECT COUNT(*) FROM ...
改为查询。
或者
int size =0;
if (rs != null)
{
rs.last(); // moves cursor to the last row
size = rs.getRow(); // get row id
}
在任何一种情况下,您都不必遍历整个数据。
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
}
好吧,如果您有 aResultSet
类型ResultSet.TYPE_FORWARD_ONLY
,您希望保持这种方式(而不是切换到 a ResultSet.TYPE_SCROLL_INSENSITIVE
orResultSet.TYPE_SCROLL_INSENSITIVE
以便能够使用.last()
)。
我建议一个非常好的和高效的 hack,在顶部添加一个包含行数的第一个虚假/虚假行。
例子
假设您的查询如下
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()
解决方案是:
stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
[速度考虑]
这里有很多 ppl 建议ResultSet.last()
,但为此您需要打开连接ResultSet.TYPE_SCROLL_INSENSITIVE
,因为 Derby 嵌入式数据库的速度比ResultSet.TYPE_FORWARD_ONLY
.
根据我对嵌入式 Derby 和 H2 数据库的微测试,SELECT COUNT(*)
在 SELECT 之前调用它要快得多。
ResultSet 大小的获取方式,无需使用 ArrayList 等
int size =0;
if (rs != null)
{
rs.beforeFirst();
rs.last();
size = rs.getRow();
}
现在你会得到大小,如果你想打印结果集,在打印之前也使用下面的代码行,
rs.beforeFirst();
这是一种进行行数计数的简单方法。
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);
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接口的运行时值,发现它几乎一直是一个ResultSetImpl。ResultSetImpl 有一个方法调用getUpdateCount()
它返回您正在寻找的值。
此代码示例应该足够了:
ResultSet resultSet = executeQuery(sqlQuery);
double rowCount = ((ResultSetImpl)resultSet).getUpdateCount()
我意识到向下转换通常是一个不安全的过程,但这种方法还没有让我失望。
今天,我使用了这个逻辑,为什么我不知道得到 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.
我遇到了同样的问题。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
最简单的方法,运行 Count(*) 查询,执行 resultSet.next() 指向第一行,然后执行 resultSet.getString(1) 来获取计数。代码 :
ResultSet rs = statement.executeQuery("Select Count(*) from your_db");
if(rs.next()) {
int count = rs.getString(1).toInt()
}
给列一个名字..
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.");
}
}