rs.last() 给出仅前向结果集的无效操作:最后

发布于 2024-12-29 02:54:54 字数 522 浏览 1 评论 0原文

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

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

但我收到 Invalid operation forforward only resultset : last 错误。结果集从 Oracle 10g 数据库获取数据。

这是我设置连接的方法:

    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);

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

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

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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

缱倦旧时光 2025-01-05 02:54:54

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

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

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

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

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");

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.

乖乖公主 2025-01-05 02:54:54
PreparedStatement ps = conn.prepareStatement ("SELECT * FROM
         EMPLOYEE_TABLE WHERE LASTNAME = ?" ,
         ResultSet.TYPE_SCROLL_INSENSITIVE , 
         ResultSet.CONCUR_UPDATABLE ,
         ResultSet.HOLD_CURSOR_OVER_COMMIT) ;

对于准备好的语句,您必须至少指定类型并发模式,以便 last()isLast() 工作。

PreparedStatement ps = conn.prepareStatement ("SELECT * FROM
         EMPLOYEE_TABLE WHERE LASTNAME = ?" ,
         ResultSet.TYPE_SCROLL_INSENSITIVE , 
         ResultSet.CONCUR_UPDATABLE ,
         ResultSet.HOLD_CURSOR_OVER_COMMIT) ;

For prepared statements, you must specify, at a minimum, both the type and the concurrency mode for last() and isLast() to work.

花期渐远 2025-01-05 02:54:54
String rowCount = "SELECT COUNT(*) as numberOfRows FROM (SELECT * FROM names)";
Statement stat = con.createStatement();
ResultSet rs = stat.executeQuery(rowCount);
int lastRow = rs.getInt("numberOfRows");
System.out.println(lastRow);

使用您的查询,而不是 SELECT * FROM name

String rowCount = "SELECT COUNT(*) as numberOfRows FROM (SELECT * FROM names)";
Statement stat = con.createStatement();
ResultSet rs = stat.executeQuery(rowCount);
int lastRow = rs.getInt("numberOfRows");
System.out.println(lastRow);

Instead of SELECT * FROM names use your query.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文