1Z0-819 JDBC 2 — Questions and Answers
Question 1: Which method of `PreparedStatement` is used to execute a SQL INSERT and return the number of affected rows?
- executeQuery()
- executeUpdate() (Correct answer)
- execute()
- executeBatch()
Correct answer: executeUpdate()
`executeUpdate()` executes DML statements (INSERT, UPDATE, DELETE) and returns the count of affected rows.
Question 2: What does `Connection.setAutoCommit(false)` do in JDBC?
- Closes the connection after each statement
- Disables connection pooling
- Begins a manual transaction requiring explicit commit or rollback (Correct answer)
- Rolls back all pending changes immediately
Correct answer: Begins a manual transaction requiring explicit commit or rollback
Setting autoCommit to false puts the connection into manual transaction mode where you must call `commit()` or `rollback()` explicitly.
Question 3: Which `ResultSet` method moves the cursor to the next row and returns `false` when no more rows exist?
- hasNext()
- nextRow()
- next() (Correct answer)
- advance()
Correct answer: next()
`ResultSet.next()` advances the cursor one row forward and returns `false` when there are no more rows.
Question 4: What exception is thrown by JDBC methods when a database access error occurs?
- IOException
- DBException
- SQLException (Correct answer)
- ConnectionException
Correct answer: SQLException
`SQLException` is the checked exception thrown by virtually all JDBC API methods when a database or driver error occurs.
Question 5: Which `DriverManager` method is used to obtain a database `Connection`?
- DriverManager.openConnection(url, user, pass)
- DriverManager.getConnection(url, user, pass) (Correct answer)
- DriverManager.connect(url, user, pass)
- DriverManager.createConnection(url, user, pass)
Correct answer: DriverManager.getConnection(url, user, pass)
`DriverManager.getConnection(url, user, password)` is the standard JDBC entry point for obtaining a database connection.
Question 6: In a `PreparedStatement`, what character is used as a placeholder for parameters?
- @
- #
- ? (Correct answer)
- $
Correct answer: ?
A question mark `?` serves as the positional placeholder in a `PreparedStatement` SQL string, set later via `setXxx()` methods.
Question 7: Which interface should you use when you need to call a stored procedure in JDBC?
- Statement
- PreparedStatement
- CallableStatement (Correct answer)
- StoredStatement
Correct answer: CallableStatement
`CallableStatement` extends `PreparedStatement` and is specifically designed to execute stored procedures via the `{call procedure_name(?)}` syntax.
Which method of `PreparedStatement` is used to execute a SQL INSERT and return the number of affected rows?