1Z0-819 JDBC 3 — Questions and Answers
Question 1: Which `ResultSet` getter retrieves a column value by its 1-based column index as a `String`?
- getString(0)
- getString(1) (Correct answer)
- getString("1")
- getColumn(1)
Correct answer: getString(1)
JDBC ResultSet column indices are 1-based, so `getString(1)` retrieves the first column as a String.
Question 2: What is the correct JDBC URL format for connecting to a local MySQL database named `testdb` on the default port?
- jdbc:mysql:localhost/testdb
- jdbc:mysql://localhost:3306/testdb (Correct answer)
- mysql://localhost:3306/testdb
- jdbc://mysql/localhost/testdb
Correct answer: jdbc:mysql://localhost:3306/testdb
A standard JDBC URL follows the pattern `jdbc:<subprotocol>://<host>:<port>/<database>`, so `jdbc:mysql://localhost:3306/testdb` is correct.
Question 3: Which method closes a `ResultSet`, `Statement`, and `Connection` together when used in a try-with-resources block?
- They must be closed manually in a finally block only
- Only Connection needs closing; the others close automatically
- Each implements AutoCloseable so all are closed automatically by try-with-resources (Correct answer)
- Only Statement and ResultSet implement AutoCloseable
Correct answer: Each implements AutoCloseable so all are closed automatically by try-with-resources
`Connection`, `Statement`, and `ResultSet` all implement `AutoCloseable`, so they are automatically closed at the end of a try-with-resources block.
Question 4: What does `Statement.executeBatch()` return?
- A single int representing total rows affected
- A ResultSet of all batch results
- An int[] where each element is the update count for the corresponding command (Correct answer)
- A boolean indicating overall batch success
Correct answer: An int[] where each element is the update count for the corresponding command
`executeBatch()` returns an `int[]` array where each element represents the update count for the corresponding SQL command added via `addBatch()`.
Question 5: Which `ResultSet` constant specifies that the cursor can scroll both forward and backward and is insensitive to database changes?
- ResultSet.TYPE_FORWARD_ONLY
- ResultSet.TYPE_SCROLL_SENSITIVE
- ResultSet.TYPE_SCROLL_INSENSITIVE (Correct answer)
- ResultSet.TYPE_BIDIRECTIONAL
Correct answer: ResultSet.TYPE_SCROLL_INSENSITIVE
`ResultSet.TYPE_SCROLL_INSENSITIVE` allows bidirectional scrolling but does not reflect changes made to the database after the ResultSet was opened.
Question 6: Which `Connection` method creates a `Statement` object for executing static SQL?
- connection.newStatement()
- connection.createStatement() (Correct answer)
- connection.buildStatement()
- connection.makeStatement()
Correct answer: connection.createStatement()
`Connection.createStatement()` creates a `Statement` object used to execute static SQL queries without parameters.
Question 7: When using `PreparedStatement`, which method binds a Java `int` value to the first placeholder?
- setInteger(1, value)
- setInt(0, value)
- setInt(1, value) (Correct answer)
- bindInt(1, value)
Correct answer: setInt(1, value)
`PreparedStatement.setInt(1, value)` binds an int to the first `?` placeholder; parameters are 1-indexed and the method is `setInt`, not `setInteger`.
Which `ResultSet` getter retrieves a column value by its 1-based column index as a `String`?