1Z0-819 JDBC 5 — Questions and Answers
Question 1: Which `ResultSet` method retrieves the value of a column as a `java.sql.Timestamp`?
- getDateTime(columnLabel)
- getTimestamp(columnLabel) (Correct answer)
- getSQLDate(columnLabel)
- getDate(columnLabel)
Correct answer: getTimestamp(columnLabel)
`ResultSet.getTimestamp(columnLabel)` retrieves a column value as a `java.sql.Timestamp`, which represents both date and time with nanosecond precision.
Question 2: What does `ResultSet.wasNull()` return after calling a getter like `getInt()`?
- true if the last column read contained a SQL NULL value (Correct answer)
- true if the ResultSet has no more rows
- true if the connection was closed during the read
- true if the column index was out of bounds
Correct answer: true if the last column read contained a SQL NULL value
`wasNull()` returns `true` if the last column value read via any getter was SQL NULL, since primitive getters like `getInt()` return 0 for NULL and you need this method to distinguish.
Question 3: Which approach correctly loads a JDBC driver in Java 11 without explicitly calling `Class.forName()`?
- You must always call Class.forName() in Java 11
- The ServiceLoader mechanism automatically discovers drivers on the classpath via META-INF/services (Correct answer)
- The driver must be registered manually with DriverManager.registerDriver()
- Java 11 removed JDBC driver support
Correct answer: The ServiceLoader mechanism automatically discovers drivers on the classpath via META-INF/services
Since JDBC 4.0 (Java 6+), drivers on the classpath are auto-loaded via the `ServiceLoader` mechanism using `META-INF/services/java.sql.Driver`, eliminating the need for `Class.forName()`.
Question 4: Which method on `CallableStatement` registers an OUT parameter before executing a stored procedure?
- setOutParameter(index, sqlType)
- registerOutParameter(index, sqlType) (Correct answer)
- declareOutParameter(index, sqlType)
- addOutParameter(index, sqlType)
Correct answer: registerOutParameter(index, sqlType)
`CallableStatement.registerOutParameter(index, sqlType)` must be called before execution to tell JDBC the type of each OUT parameter to be retrieved afterward.
Question 5: What SQL syntax is used in JDBC to call a stored procedure with one IN and one OUT parameter?
- "{call myProc(?, ?)}" (Correct answer)
- "EXEC myProc(?, ?)"
- "CALL myProc(IN ?, OUT ?)"
- "{ myProc(?, ?) }"
Correct answer: "{call myProc(?, ?)}"
JDBC uses the escape syntax `{call procedureName(?, ?)}` to call stored procedures in a portable, database-independent way.
Question 6: Which `Connection` transaction isolation level offers the strongest consistency guarantee, preventing dirty reads, non-repeatable reads, and phantom reads?
- TRANSACTION_READ_COMMITTED
- TRANSACTION_REPEATABLE_READ
- TRANSACTION_SERIALIZABLE (Correct answer)
- TRANSACTION_READ_UNCOMMITTED
Correct answer: TRANSACTION_SERIALIZABLE
`TRANSACTION_SERIALIZABLE` is the highest isolation level, ensuring full serializability by preventing dirty reads, non-repeatable reads, and phantom reads.
Question 7: What is returned by `PreparedStatement.execute()` when the SQL is a SELECT query?
- The number of rows in the result set
- true, indicating a ResultSet is available via getResultSet() (Correct answer)
- false, indicating no rows were returned
- A ResultSet directly
Correct answer: true, indicating a ResultSet is available via getResultSet()
`execute()` returns `true` when the first result is a `ResultSet` (e.g., for SELECT), after which you call `getResultSet()` to retrieve it.
Which `ResultSet` method retrieves the value of a column as a `java.sql.Timestamp`?