本文介绍了rs.last()为仅向前结果集提供无效操作:last的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过以下方式获得结果集的行数:

I am trying to get the row count of a result set by:

rs.last();
int row_count = rs.getRow();

但我得到仅转发结果集的无效操作:last 错误。结果集从Oracle 10g数据库获取数据。

but im getting an Invalid operation for forward only resultset : last error. The result set is getting its data from an Oracle 10g database.

以下是我设置连接的方式:

Here is how i set up my connection:

    Class.forName("oracle.jdbc.driver.OracleDriver");
    String connectionString = "jdbc:oracle:thin:@" + oracle_ip_address + ":" + oracle_db_port + ":" + oracle_db_sid;
    Connection conn = DriverManager.getConnection(connectionString, oracle_db_username, oracle_db_password);


推荐答案

ResultSet.last() 和其他绝对索引查询操作仅在结果集可滚动时可用;否则,您只能通过仅向前结果集逐个迭代。

ResultSet.last() and other "absolutely-indexed" query operations are only available when the result set is scrollable; otherwise, you can only iterate one-by-one through the forward-only result set.

以下示例(来自)演示如何创建可滚动的结果集

The following example (from the javadocs) demonstrates how to create a scrollable ResultSet.

Statement stmt = con.createStatement(
    ResultSet.TYPE_SCROLL_INSENSITIVE,
    ResultSet.CONCUR_READ_ONLY
);
ResultSet rs = stmt.executeQuery("SELECT a, b FROM TABLE2");

请记住,使用可滚动查询会对性能产生影响。如果此特定 ResultSet 的目标仅是获取其最后一个值,请考虑优化查询以仅返回该结果。

Keep in mind that there are performance implications to using scrollable queries. If the goal of this particular ResultSet is only to grab its last value, please consider refining your query to return only that result.

这篇关于rs.last()为仅向前结果集提供无效操作:last的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 04:45