Exam 1z0-809 All QuestionsBrowse all questions from this 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?

    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
adnano1234

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

Tarik2190Option: C

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

r1muka5Option: C

Correct answer is C.

DarGrinOption: C

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

DarGrinOption: D

java.lang.NullPointerException , tested

DarGrin

sorry. answer is C

steefaandOption: C

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

WilsonKKerllOption: D

Answer : java.lang.NullPointerException

mevltOption: A

The answer is A. Tested.

DestroyerOption: D

Answer is "A NullPointerException is thrown as runtime."