Oracle Certified Professional Java Se 11 Developer · Free Practice Question Hard
Question 100
Question ID: UKOCP44683
Given structure of MESSAGES table:
MESSAGES (msg1 varchar(100), msg2 varchar(100))
MESSAGES table contains below records:
'Happy New Year!', 'Happy Holidays!'
Given code of Test.java file:
- package com.udayankhattry.ocp;
- import java.sql.*;
- public class Test {
- public static void main(String[] args) throws SQLException {
- var url = "jdbc:mysql://localhost:3306/ocp";
- var user = "root";
- var password = "password";
- var query = "DELETE FROM MESSAGES";
- try (var con = new Connection(url, user, password);
- var stmt = con.createStatement())
- {
- System.out.println(stmt.executeUpdate(query));
- }
- }
- }
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 0
- B 1
- C Compilation error
- D An exception is thrown at runtime
Reveal correct answer
Correct answer: C
Explanation
UKOCP44683:
Connection is an interface, hence new Connection(url, user, password); causes compilation error.
Correct way to create Connection object in this case will be: DriverManager.getConnection(url, user, password);
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
