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

问题描述

我试图通过以下方式获取结果集的行数:

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

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

但我收到一个 Invalid operation for forward only resultset : 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() 和其他绝对索引"查询操作仅在结果集可滚动时可用;否则,您只能对 forward-only 结果集进行一一迭代.

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.

以下示例(来自 javadocs) 演示了如何创建可滚动的 ResultSet.

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-22 18:10