How many rows are added to the colors table from running the following?

How many rows are added to the colors table from running the following?

try (Connection conn = DriverManager.getConnection(url); Statement stmt = conn.createStatement()) {
    conn.setAutoCommit(false);
    stmt.executeUpdate("insert into colors values ('red')");
    stmt.executeUpdate("insert into colors values ('blue')");
    conn.commit();
    conn.setAutoCommit(true);
    stmt.executeUpdate("insert into colors values ('green')");
}
A. None
B. One
C. Two
D. Three

Answer

B. One

Explanation:

The code turns off automatic committing, so the inserts for red and blue are not immediately made. 
The rollback(a) statement actually prevents them from being committed. Then automatic commit is turned back on and one insert is made, making Option B the answer.

Comments