From JAVA 7 API https://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html#absolute(int) we get:
"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"
But JAVA SE 8 II certification book puts in chapter 10 (JDBC), exercise 18:
String sql = "select name from animal order by id";
try (Connection conn = DriverManager.getConnection("jdbc:derby:zoo");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql)) {
rs.absolute(0);
And no exception is thrown according to the book answer. So, is it possible to move forward in a TYPE_FORWARD_ONLY ResultSet with the absolute function?
No, you can't expect this to work, and given a strict interpretation of JDBC it is even incorrect for a driver to do that for a TYPE_FORWARD_ONLY
result set.
The javadoc explicitly says for ResultSet#absolute(int)
:
Throws:
SQLException
- if a database access error occurs; this method is called on a closed result set or the result set type isTYPE_FORWARD_ONLY
And that is not unexpected. Think what would happen if you first call absolute(5)
and then absolute(1)
on a real TYPE_FORWARD_ONLY
result set. The first would succeed and the second call wouldn't: that is inconsistent behavior, and that should be avoided.
So the code in that book is wrong, and the author is relying (probably without even knowing it) on the specific database driver used to silently upgrade the TYPE_FORWARD_ONLY
to TYPE_SCROLL_INSENSITIVE
(which is what some drivers do in - for example - auto commit).