Maurice
Maurice

Reputation: 7381

calling absolute() on a resultset that is not set to TYPE_SCROLL_SENSITIVE or INSENSITIVE

According to my OCP study book the following does not throw an exception:

try(Connection conn = DriverManager.getConnection("jdbc:derby:zoo");
  Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql)){

rs.absolute(0);
rs.next();
System.out.println(rs.getString(1));
}

As you can see conn.createStatement() doesen't have any parameters like ResultSet.TYPE_SCROLL_INSENSITIVE So the resultset should only be able to move forward one row at a time right? Yet they say no exception is thrown. So is this an error in the OCP book or am I missing something here?

Regards

Upvotes: 1

Views: 1219

Answers (2)

Steve Chambers
Steve Chambers

Reputation: 39424

So is this an error in the OCP book or am I missing something here?

Short answer: An error in the OCP book.

Longer answer below...

I had exactly the same question in my head when encountering this so tried it out myself. Sure enough, on running the code the following exception was thrown:

Exception in thread "main" java.sql.SQLException: The 'absolute()' method is only allowed on scroll cursors.
  at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source)
  at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source)
  at org.apache.derby.impl.jdbc.Util.generateCsSQLException(Unknown Source)
  at org.apache.derby.impl.jdbc.Util.generateCsSQLException(Unknown Source)
  at org.apache.derby.impl.jdbc.EmbedResultSet.checkScrollCursor(Unknown Source)
  at org.apache.derby.impl.jdbc.EmbedResultSet.absolute(Unknown Source)
  at com.stevectest.Main.main(Main.java:11)
Caused by: ERROR XJ061: The 'absolute()' method is only allowed on scroll cursors.
  at org.apache.derby.iapi.error.StandardException.newException(Unknown Source)
  at org.apache.derby.impl.jdbc.SQLExceptionFactory.wrapArgsForTransportAcrossDRDA(Unknown Source)
  ... 7 more

This aligns with the absolute method's Javadoc, which states:

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

Also found there is an errata entry on the selikoff.net OCP Study Guide webpage for this:

572 Chapter 10 #18: The correct answer should be E. The answer should be E, not A because the result set type is not scollable. Incorrect answer Mike Constantin 2016-02-05 Pending

Upvotes: 1

N00b Pr0grammer
N00b Pr0grammer

Reputation: 4647

Oracle documentation can give you more details on why this won't create any exceptions. As per the documentation, there are default values to the parameters like 'type' and 'concurrency'.

Statement createStatement() throws SQLException

Creates a Statement object for sending SQL statements to the database. SQL statements without parameters are normally executed using Statement objects. If the same SQL statement is executed many times, it may be more efficient to use a PreparedStatement object.

Result sets created using the returned Statement object will by default be type TYPE_FORWARD_ONLY and have a concurrency level of CONCUR_READ_ONLY. The holdability of the created result sets can be determined by calling getHoldability().

Returns:

a new default Statement object

Throws: SQLException - if a database access error occurs or this method is called on a closed connection

And as @MarkRotteveel already mentioned in his comment,

Also, some driver implementations are lenient and allow more than is required by JDBC, eg some drivers allow you to use absolute with a forward-only result set, as long as the row index is the current or higher

Hope this answers your question!

Upvotes: 1

Related Questions