rs.last() gives Invalid operation for forward only resultset : last
Solution 1
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.
Solution 2
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.
Mike
Updated on March 08, 2020Comments
-
Mike about 4 years
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);
-
Paresh about 8 yearsAs per latest specification, we have to add two parameters to the createStatement() -
Statement statement = con.createStatement(resultSetType, resultSetConcurrency);
as described by cheeken, we have to passResultSet.TYPE_SCROLL_INSENSITIVE
but with that we also have to pass one more parameter - please refer this -resultSetType a result set type; one of ResultSet.TYPE_FORWARD_ONLY, ResultSet.TYPE_SCROLL_INSENSITIVE, or ResultSet.TYPE_SCROLL_SENSITIVE resultSetConcurrency a concurrency type; one of ResultSet.CONCUR_READ_ONLY or ResultSet.CONCUR_UPDATABLE
-
Squareoot over 7 yearsPlease help me it's saying
The method createStatement() in the type Connection is not applicable for the arguments (int)
when I try to executeStatement stmt = con.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE );
-
isaac.hazan almost 7 yearsisLast() does not require the cursor to be scrollable. last() requires it.
-
Skynet almost 7 years@StringForever refer to what PareshKumar has mentioned.
-
Mark Rotteveel over 6 years@danielad Actually, that method signature has always required two parameters. The code in the original was always incorrect. I have now corrected it (and updated the broken link).
-
Vadzim over 5 years@isaac.hazan, not true for any driver: stackoverflow.com/questions/10597068/…
-
Dhruv Singhal almost 5 yearsResultSet.CONCUR_UPDATABLE is NOT recommended to be used in commercial projects. Please avoid if possible.