1Z0-819 JDBC 4 — Questions and Answers
Question 1: What happens if you call `Connection.rollback()` when `autoCommit` is `true`?
- It rolls back all changes since the last commit
- It has no effect
- It throws an SQLException (Correct answer)
- It disables autoCommit automatically
Correct answer: It throws an SQLException
Calling `rollback()` when `autoCommit` is `true` throws an `SQLException` because there is no active transaction to roll back.
Question 2: Which `DatabaseMetaData` method retrieves a `ResultSet` of all tables in the connected database?
- getTables(catalog, schema, tableNamePattern, types) (Correct answer)
- listTables()
- getTableNames(schema)
- fetchTables(catalog)
Correct answer: getTables(catalog, schema, tableNamePattern, types)
`DatabaseMetaData.getTables(catalog, schemaPattern, tableNamePattern, types)` returns a ResultSet describing the tables available in the connected database.
Question 3: Which `SavePoint` feature allows partial rollback within a transaction?
- Connection.createSavepoint() marks a point you can roll back to without aborting the whole transaction (Correct answer)
- Savepoints are not supported in standard JDBC
- Connection.setSavePoint() commits work up to that point
- Savepoints require autoCommit=true to function
Correct answer: Connection.createSavepoint() marks a point you can roll back to without aborting the whole transaction
`Connection.setSavepoint()` marks an intermediate point in a transaction that you can roll back to with `rollback(savepoint)` while keeping earlier changes.
Question 4: Which `ResultSet` concurrency constant allows updates to be made through the ResultSet object?
- ResultSet.CONCUR_READ_ONLY
- ResultSet.CONCUR_UPDATABLE (Correct answer)
- ResultSet.CONCUR_WRITE_ONLY
- ResultSet.CONCUR_MUTABLE
Correct answer: ResultSet.CONCUR_UPDATABLE
`ResultSet.CONCUR_UPDATABLE` allows the caller to update rows in the database directly through the ResultSet using update methods.
Question 5: What is the purpose of `DataSource` compared to `DriverManager` in JDBC?
- DataSource is only for embedded databases
- DataSource provides connection pooling and is the preferred enterprise approach (Correct answer)
- DataSource replaces PreparedStatement for batch operations
- DataSource and DriverManager are functionally identical
Correct answer: DataSource provides connection pooling and is the preferred enterprise approach
`DataSource` is the preferred way to obtain connections in enterprise applications because it supports connection pooling, improving performance and scalability.
Question 6: Which JDBC isolation level prevents dirty reads but still allows non-repeatable reads?
- TRANSACTION_READ_UNCOMMITTED
- TRANSACTION_READ_COMMITTED (Correct answer)
- TRANSACTION_REPEATABLE_READ
- TRANSACTION_SERIALIZABLE
Correct answer: TRANSACTION_READ_COMMITTED
`TRANSACTION_READ_COMMITTED` prevents dirty reads (reading uncommitted data) but allows non-repeatable reads since data can change between reads in the same transaction.
Question 7: Which method on `Statement` adds a SQL command to a pending batch without executing it immediately?
- queueBatch(sql)
- addBatch(sql) (Correct answer)
- appendBatch(sql)
- stageBatch(sql)
Correct answer: addBatch(sql)
`Statement.addBatch(sql)` appends the SQL command to the current batch list; the batch is not sent to the database until `executeBatch()` is called.
What happens if you call `Connection.rollback()` when `autoCommit` is `true`?