Search code examples
javadatabasejdbcresultset

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


Solution

  • 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