[英]How do I get the size of a java.sql.ResultSet?
這不應該是一個非常簡單的操作嗎? 但是,我看到既沒有size()
也沒有length()
方法。
改為執行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
}
好吧,如果你有一個ResultSet
類型的ResultSet.TYPE_FORWARD_ONLY
你想保持這種方式(而不是切換到ResultSet.TYPE_SCROLL_INSENSITIVE
或ResultSet.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);
【速度考慮】
這里的很多人都建議使用ResultSet.last()
但為此你需要將連接作為ResultSet.TYPE_SCROLL_INSENSITIVE
打開,對於 Derby 嵌入式數據庫,它比ResultSet.TYPE_FORWARD_ONLY
慢10 倍。
根據我對嵌入式 Derby 和 H2 數據庫的微測試,在 SELECT 之前調用SELECT COUNT(*)
明顯更快。
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.");
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.