Oracle Certified Professional Java Se 11 Developer · Free Practice Question Medium
Question 23
Question ID: UKOCP32218
Given structure of EMPLOYEE table:
EMPLOYEE (ID integer, FIRSTNAME varchar(100), LASTNAME varchar(100), SALARY real, PRIMARY KEY (ID))
EMPLOYEE table contains below records:
- 101 John Smith 12000
- 102 Sean Smith 15000
- 103 Regina Williams 15500
- 104 Natasha George 14600
Given code of Test.java file:
- package com.udayankhattry.ocp;
- import java.sql.*;
- import java.util.Properties;
- public class Test {
- public static void main(String[] args) throws Exception {
- var url = "jdbc:mysql://localhost:3306/ocp";
- var prop = new Properties();
- prop.put("user", "root");
- prop.put("password", "password");
- var query = "Select ID, FIRSTNAME, LASTNAME, SALARY FROM EMPLOYEE ORDER BY ID";
- Class.forName(url);
- try (var con = DriverManager.getConnection(url, prop);
- var stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
- var rs = stmt.executeQuery(query);) {
- rs.relative(1);
- System.out.println(rs.getString(2));
- }
- }
- }
Also assume:
URL is correct and db credentials are: root/password.
SQL query is correct and valid.
The JDBC 4.2 driver jar is configured in the classpath.
What is the result?
- A John
- B Sean
- C Smith
- D An exception is thrown at runtime
Reveal correct answer
Correct answer: D
Explanation
UKOCP32218:
Variable 'url' infers to String type, variable 'prop' infers to Properties type. Variable 'con' infers to Connection type, variable 'stmt' infers to Statement type and variable 'rs' infers to ResultSet type.
It is assumed that JDBC 4.2 driver is configured in the classpath, hence Class.forName(String) is not required. But no harm in using Class.forName(String).
Class.forName(String) expects fully qualified name of the class but in this case url refers to database url and not fully qualified name of the class, hence ClassNotFoundException is thrown at runtime.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
