rs.last() gives Invalid operation for forward only resultset : last

后端 未结 2 2050
無奈伤痛
無奈伤痛 2020-12-06 04:37

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

相关标签:
2条回答
  • 2020-12-06 05:05
    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.

    0 讨论(0)
  • 2020-12-06 05:29

    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.

    0 讨论(0)
提交回复
热议问题