Java OCA OCP Practice Question 2045

Question

Given a scrollable updatable ResultSet that contains the following, what does the code snippet output?.

color count
character varying(255) integer
black 20
blue 5
red0
rs = stmt.executeQuery("select * from pens"); 
rs.afterLast(); 
rs.previous(); 
rs.updateInt(2, 10); 
rs.updateRow(); 

rs = stmt.executeQuery("select * from pens where color = 'red'"); 
while (rs.next()) 
   System.out.println(rs.getInt(2)); 
  • A. 0
  • B. 10
  • C. The code does not compile.
  • D. The code compiles but throws an exception at runtime.


B.

Note

This code shows how to properly update a ResultSet.

Note that it calls updateRow() so the changes get applied in the database.

This allows the SELECT query to see the changes and output 10.

Option B is correct.

Remember that unlike this code, you should always close a ResultSet when you open it in real code.




PreviousNext

Related