0

I have the following code:

//rs is a ResultSet from a prepared statement
rs.last();
this.resultCount = rs.getRow();
rs.beforeFirst();

If I execute that code after i executed a few rs.next(), then the rs.beforeFirst() is wrong.

So my question is: how can I get back to the current position and not to the beforeFirst position?

Dushyant Tankariya
  • 1,432
  • 3
  • 11
  • 17
Robin Kreuzer
  • 162
  • 1
  • 10
  • Don't rely on `getLast()`. It can be a pretty expensive operation (because the driver may have to read all rows), and it isn't supported on forward-only result sets. Why do you want to know total row count? It is usually better to try to avoid the need to know this, or alternatively consider just explicitly querying for the row count. – Mark Rotteveel Sep 24 '19 at 10:44
  • @MarkRotteveel so you would recommend, that i do two queries? One SELECT count..... query and then my normal query if i need both: the size of the result and the result? But i understand now, that getLast() can be very expensive, if the driver dont get the position on a direct way – Robin Kreuzer Sep 24 '19 at 10:52
  • My primary recommendation would be to find a way to not need to know the total count. Usually the information value of that is pretty low, so avoiding the cost is better. – Mark Rotteveel Sep 24 '19 at 10:56
  • @MarkRotteveel yes i guess you are right – Robin Kreuzer Sep 24 '19 at 11:08

1 Answers1

0

Question: "how can I get back to the current position and not to the before the first position."

Answer: You can use resultSet.absolute(rowIndex);


Detailed explanation:
Moves the cursor to the given row number in this ResultSet object.
If the row number is positive, the cursor moves to the given row number with respect to the beginning of the result set. The first row is row 1, the second row 2, and so on.

If the given row number is negative, the cursor moves to an absolute row position with respect to the end of the result set. For example, calling the method absolute(-1) positions the cursor on the last row; calling the method absolute(-2) moves the cursor to the next-to-last row, and so on.

If the row number specified is zero, the cursor is moved to before the first row.


However, you can use absolumte(rowIndex) in your program such as,

int currRowIndex = resultSet.getRow();
resultSet.last(); // <- can throw if resultSet type is TYPE_FORWARD_ONLY
this.resultCount = resultSet.getRow();
resultSet.absolute(currRowIndex); // <- It will allow you to set cursor position.

Warning: When the use of last() it can throw if the resultSet type is TYPE_FORWARD_ONLY.

last
boolean last() throws SQLException
Moves the cursor to the last row in this ResultSet object.<be>

Returns:
 - true if the cursor is on a valid row;
 - false if there are no rows in the result set

Throws:
 - SQLException: if a database access error occurs; this method is called on a closed result set or the result set type is TYPE_FORWARD_ONLY

 - SQLFeatureNotSupportedException: if the JDBC driver does not support
   this method
Dushyant Tankariya
  • 1,432
  • 3
  • 11
  • 17
  • ok thank you, maybe you can take a look into this here:https://stackoverflow.com/questions/58071915/resultset-has-row-count-value-more-than-integer-max-value-how If i overflow the row integer, then i would get problems, if i save the currentPosition rowIndex, if that current position is > MAX_INTEGER – Robin Kreuzer Sep 24 '19 at 10:47
  • is that resultSet.beforeFirst(); before resultSet.absolute..... needed? – Robin Kreuzer Sep 24 '19 at 10:49
  • Nope, My mistake. Improved it. Thank you for help me to improve my answer quality. And yes, I've also referred your SO Question same as here you've posted. If you don't mind can you tell me why you need Total of resultSet rows? – Dushyant Tankariya Sep 24 '19 at 10:51
  • i write a small api, that encapsulate some jdbc code. That api contains the method getRowCount(). if i later really need that, is a good question. But my problem here is more that i could have a resultSet bigger Than Max_Integer and from JDBC the resultSet that is be incorrect if the results quantity is bigger than MAX_INTEGER. So my problem is that the ResultSet from JDBC is at the semantic level incorrect – Robin Kreuzer Sep 24 '19 at 11:08
  • Yes, I agree here, I'm curious to know what is the purpose to take total That's why my question is why you need the RowCount()? are you planning to use that total for any iteration process? – Dushyant Tankariya Sep 24 '19 at 11:16