Skip to content
Sharp

SQL for Java Devs · interview question

Consider the following code snippet. What will happen if one of the statements in the batch fails?

Multiple choiceMediumchapter-8-curatedpremiumsql-for-java-devsbatching
java
PreparedStatement pstmt = connection.prepareStatement(sql);
pstmt.addBatch();
pstmt.executeBatch();
Choose one answer
Answer and explanation

Correct answer

A. The entire batch is rolled back.

Why

If one of the statements in a batch fails, the entire batch is rolled back by default unless specific error handling is implemented. This ensures data consistency by not applying partial updates.

Test yourself on SQL for Java Devs

A free test with instant, explained results. No account needed.

Start free test
All SQL for Java Devs questions →