I believe that there is a mistake in this review question. It reads as follows:
Suppose there are two rows in the table before this code is run, and executeUpdate() runs without error. How many rows are in the table after the code completes?
A. Two
B. Three
C. The code does not compile.
D. The code throws an exception.
The solution claims that the answer is:
B. The first line enables autocommit mode. This is the default and means to commit immediately after each update. When the rollback() runs, there are no uncommitted statements, so there is nothing to roll back. This gives us the initial two rows in addition to the inserted one making option B correct. If setAutoCommit(false) were called, option A would be the answer. The ResultSet types are just there to mislead you. Any types are valid for executeUpdate() since no ResultSet is involved.
However, the documentation for
Java SE 17's Connection::rollback
https://docs.oracle.com/en/java/javase/17/docs/api/java.sql/java/sql/Connection.html#rollback() states that rollback throws an SQLException when
Throws:
SQLException - if a database access error occurs, this method is called while participating in a distributed transaction, this method is called on a closed connection or this Connection object is in auto-commit mode
As the Connection in the example is in auto-commit mode when rollback is called, any compliant implementation should throw an SQLException, meaning D should be the correct answer, no?
// EDIT: I checked the existing errata list, but not the forum history. I see that this was actually discussed in the context of one of the earlier books, where the (if you ask me, correct choice) decision was made to answer according to the javadoc api, and not some specific non-compliant) implementation.
https://coderanch.com/t/655693/certification/Calling-commit-rollback-autocommit-mode