在未设置为 TYPE_SCROLL_SENSITIVE 或 INSENSITIVE 的结果集上调用 absolute()

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

根据我的OCP学习书,以下不会抛出异常:

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));
}

如您所见,conn.createStatement() 没有像 ResultSet.TYPE_SCROLL_INSENSITIVE 这样的任何参数,所以结果集应该一次只能向前移动一行,对吧?然而他们说不会抛出异常。这是 OCP 书中的错误还是我在这里遗漏了什么?

此致

Oracle documentation 可以为您提供更多详细信息,说明为什么这不会产生任何异常。根据文档,'type' 和 '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

正如@MarkRotteveel 在他的评论中提到的那样,

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

希望这能回答您的问题!

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

简答:OCP书中的一个错误。

下面的答案更长...

遇到这个问题时,我脑子里有完全相同的问题,所以自己尝试了一下。果然,在 运行 代码中抛出了以下异常:

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

这与 absolute method's Javadoc 一致,其中指出:

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

还发现 selikoff.net OCP Study Guide webpage 上有一个勘误条目:

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