Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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); 
like image 519
Mike Avatar asked Jan 25 '12 17:01

Mike


People also ask

What is forward only ResultSet?

A forward only updatable result set maintains a cursor which can only move in one direction (forward), and also update rows.

What is the function of RS next () in a ResultSet called RS?

The next() method of the ResultSet interface moves the pointer of the current (ResultSet) object to the next row, from the current position.

How do I get the last row in ResultSet?

You can move the cursor of the ResultSet object to the last row from the current position, using the last() method of the ResultSet interface. This method returns a boolean value specifying whether the cursor has been moved to the last row successfully.


1 Answers

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.

like image 73
cheeken Avatar answered Sep 29 '22 01:09

cheeken