Java OCA OCP Practice Question 2023

Question

Suppose the blue database exists and we are using a JDBC 4.0 driver.

Which is the outcome of this code?

String url = "jdbc:derby:blue"; 
Class.forName(url); 
try (Connection conn = DriverManager.getConnection(url); 
   Statement stmt = conn.createStatement(); 
   ResultSet rs = stmt.executeQuery("select count(*) from sky")) { 
   System.out.println(rs.getInt(1)); 
} 
  • A. It runs successfully and prints the number of rows in the sky table.
  • B. It throws a ClassNotFoundException.
  • C. It throws a SQLException.
  • D. It does not compile.


B.

Note

JDBC 4.0 allows, but does not require, a call to the Class.forName() method.

However, since it is in the code, it needs to be correct.

This method is expecting a fully qualified class name of a database driver, not the JDBC URL.

The Class.forName() method throws a ClassNotFoundException, and Option B is the answer.




PreviousNext

Related