Java OCA OCP Practice Question 2037

Question

Assume the database exists with all referenced table and column names.

Which is a true statement when a JDBC 4.0 driver is used?.

String url = "jdbc:derby:myDB"; 
try (Connection conn = DriverManager.getConnection(url); 
   Statement stmt = conn.createStatement( 
       ResultSet.CONCUR_READ_ONLY, 
       ResultSet.TYPE_SCROLL_INSENSITIVE); 
   ResultSet rs = stmt.execute( /*  w w w  .  jav a  2  s  .c  o m*/
      "select total from myTable where type = 'rain'")) { 

   System.out.println(rs.getString("total")); 
} 
  • A. There is a compiler error on the line of code that creates the Statement.
  • B. There is a compiler error on the line of code that creates the ResultSet.
  • C. This code compiles and runs without error.
  • D. This code throws a SQLException at runtime.


B.

Note

The parameters to createStatement() are backward.

However, they still compile because both are of type int.

This means the code to create the Statement does compile, and Option A is incorrect.

Next comes the code to create the ResultSet.

While both execute() and executeQuery() can run a SELECT SQL statement, they have different return types.

Only executeQuery() can be used in this example.

The code does not compile because the execute() method returns a boolean, and Option B is correct.

If this was fixed, Option D would be the answer because rs.next() is never called.




PreviousNext

Related