Java 如何在 JDBC 中获取行数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/162571/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get the row count in JDBC?
提问by Steve McLeod
I've executed a JDBC query to obtain a resultset. Before iterating over it, I'd like to quickly find out how many rows were returned. How can I do this with high performance?
我已经执行了一个 JDBC 查询来获取结果集。在迭代之前,我想快速找出返回了多少行。我怎样才能以高性能做到这一点?
I'm using Java 6, Oracle 11g, and the latest Oracle JDBC drivers.
我使用的是 Java 6、Oracle 11g 和最新的 Oracle JDBC 驱动程序。
采纳答案by cletus
You're going to have to do this as a separate query, for example:
您将不得不将此作为单独的查询执行,例如:
SELECT COUNT(1) FROM table_name
Some JDBC drivers might tell you but this is optional behaviour and, more to the point, the driver may not know yet. This can be due to how the query is optimised eg two example execution strategies in Oracle are to get all rows as quickly as possible or to get the first row as quickly as possible.
一些 JDBC 驱动程序可能会告诉您,但这是可选行为,更重要的是,驱动程序可能还不知道。这可能是由于查询是如何优化的,例如 Oracle 中的两个示例执行策略是尽快获取所有行或尽快获取第一行。
If you do two separate queries (one a count and the other the query) then you'll need to do them within the same transaction. This will work well on Oracle but can be problematic on other databases (eg SQL Server will either show you uncommitted data or block on an external uncommitted update depending on your isolation level whereas Oracle supports an isolation level that gives you a transactionally consistent view of the data without blocking on external updates).
如果您执行两个单独的查询(一个是计数,另一个是查询),那么您需要在同一个事务中执行它们。这在 Oracle 上运行良好,但在其他数据库上可能会出现问题(例如,SQL Server 将根据您的隔离级别向您显示未提交的数据或阻止外部未提交的更新,而 Oracle 支持的隔离级别可为您提供事务上一致的视图数据而不会阻止外部更新)。
Normally though it doesn't really matter how many rows there are. Typically this sort of query is either batch processed or paged and either way you have progress information in the form of rows loaded/processed and you can detect the end of the result set (obviously).
通常,尽管有多少行并不重要。通常,这种查询要么是批处理的,要么是分页的,无论哪种方式,您都可以以加载/处理的行的形式获得进度信息,并且您可以检测到结果集的结尾(显然)。
回答by Jon Skeet
Short answer: you can't.
简短的回答:你不能。
Long answer: you can't, partly because the database may be lazily evaluating the query, only returning rows as you ask for them.
长答案:你不能,部分原因是数据库可能懒惰地评估查询,只在你要求时返回行。
EDIT: Using a scrollable ResultSet you can :)
编辑:使用可滚动的结果集,您可以:)
Indeed, I asked this very question in the Java databases newsgroup a long time ago (back in 2001!) and had some helpful responses.
事实上,我很久以前(早在 2001 年!)就在 Java 数据库新闻组中问过这个问题,并得到了一些有用的答复。
回答by Tom Hawtin - tackline
If your driver supports it(!), you can call ResultSet.afterLast()
ResultSet.getRow()
ResultSet.beforeFirst()
. Performance may or may not be good.
如果您的驱动程序支持(!),您可以调用ResultSet.afterLast()
ResultSet.getRow()
ResultSet.beforeFirst()
. 性能可能好也可能不好。
A better solution would be to rewrite your algorithm not to require the size up front.
更好的解决方案是重写您的算法,而不需要预先确定大小。
回答by thezar
ResultSet rs = stmt.executeQuery(sql);
int rowCount = rs.last() ? rs.getRow() : 0; // Number of rows in result set. Don't forget to set cyrsor to beforeFirst() row! :)
回答by MindBrain
Code:
代码:
//Create a Statement class to execute the SQL statement
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT COUNT(*) AS COUNT FROM
TABLENAME");
while(rs.next()) {
System.out.println("The count is " + rs.getInt("COUNT"));
}
//Closing the connection
con.close();
回答by user2594537
To get the number of rows from JDBC:
要从 JDBC 获取行数:
ResultSet rs = st.executeQuery("select count(*) from TABLE_NAME");
rs.next();
int count = rs.getInt(1);
回答by v8-E
Without ternary operator
没有三元运算符
rs.last(); // Moves the cursor to the last row in this ResultSet object.
int rowCount = rs.getRow(); //Retrieves the current row number.
rs.beforeFirst(); //Moves the cursor to the front of this ResultSet object,just before the first row.
With ternary operator one line
三元运算符一行
int rowCount = rs.last() ? rs.getRow() : 0;
rs.beforeFirst();