Java OCA OCP Practice Question 2070

Question

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); // w  w w . ja  v  a  2 s  .c om
   stmt.executeUpdate("insert into colors values ('red')"); 
   stmt.executeUpdate("insert into colors values ('blue')"); 
   conn.rollback(); 
   conn.setAutoCommit(true); 
   stmt.executeUpdate("insert into colors values ('green')"); 
   conn.rollback(); 
} 
  • A. None
  • B. One
  • C. Two
  • D. Three


B.

Note

The code turns off automatic committing, so the inserts for red and blue are not immediately made.

The rollback() statement says to prevent any changes made from occurring.

This gets rid of red and blue.

Then automatic commit is turned back on and the one insert for green is made.

The final rollback has no effect since the commit was automatically made.

Since there was one row added, Option B is the answer.




PreviousNext

Related