1z0-809 Exam QuestionsBrowse all questions from this exam

1z0-809 Exam - Question 29


Given the structure of the STUDENT table:

Student (id INTEGER, name VARCHAR)

Given:

public class Test {

static Connection newConnection =null;

public static Connection get DBConnection () throws SQLException { try (Connection con = DriveManager.getConnection(URL, username, password)) { newConnection = con;

}

return newConnection;

}

public static void main (String [] args) throws SQLException { get DBConnection ();

Statement st = newConnection.createStatement();

st.executeUpdate("INSERT INTO student VALUES (102, "˜Kelvin')");

}

}

Assume that:

The required database driver is configured in the classpath.

The appropriate database is accessible with the URL, userName, and passWord exists.

The SQL query is valid.

What is the result?

Show Answer
Correct Answer: C

The program attempts to use a Connection object that was closed due to the try-with-resources statement. When 'newConnection' is used to createStatement(), it is closed, leading to a SQLException when attempting to execute the SQL query.

Discussion

9 comments
Sign in to comment
adnano1234
Jan 13, 2020

Answer is: ConnectionIsClosedException Try with resources has, as a result, closed the connection.

Tarik2190Option: C
Apr 24, 2021

Answer C: cannot be NullPointerException, since the object of connection is still there, it is just closed, which means SQLException

r1muka5Option: C
Mar 14, 2023

Correct answer is C.

DestroyerOption: D
Dec 30, 2019

Answer is "A NullPointerException is thrown as runtime."

mevltOption: A
Feb 28, 2022

The answer is A. Tested.

WilsonKKerllOption: D
Mar 11, 2022

Answer : java.lang.NullPointerException

steefaandOption: C
Feb 6, 2024

C is correct since connection is not null but closed and SQLException is thrown.

DarGrinOption: D
May 24, 2024

java.lang.NullPointerException , tested

DarGrin
Jun 12, 2024

sorry. answer is C

DarGrinOption: C
Jun 9, 2024

Answer is C, tested Exception in thread "main" java.sql.SQLNonTransientConnectionException: Connection is closed