Java OCA OCP Practice Question 2053

Question

Suppose the myTable database does not exist and we are using a JDBC 4.0 driver.

Which is the outcome of this code?.

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


C.

Note

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

Since the database does not exist, DriverManager.getConnection() throws a SQLException, and Option C is the answer.




PreviousNext

Related