Java OCA OCP Practice Question 2498

Question

Which of the following options demonstrates the correct use and closing of database resources?.

a  public static void main(String[] args) throws Exception {
       Connection con = getConnection();
       Statement statement = con.createStatement();
       ResultSet rs = statement.executeQuery("SELECT name, email FROM doctor");
    }//w w w . jav  a2 s.  c  o m

b  public static void main(String[] args) throws Exception {
       try (Connection con = getConnection();
       Statement statement = con.createStatement();
       ResultSet rs = statement.executeQuery("SELECT name, email FROM doctor");) {}
   }

c  public static void main(String[] args) throws Exception {
       try {
           Connection con = getConnection();
           Statement statement = con.createStatement();
           ResultSet rs = statement.executeQuery("SELECT name, email FROM doctor");
       }
       catch (SQLException e) {
           rs.close();
           statement.close();
           con.close();
       }
   }

d  public static void main(String[] args) throws Exception {
       try {
           Connection con = getConnection();
           Statement statement = con.createStatement();
           ResultSet rs = statement.executeQuery("SELECT name, email FROM doctor");
       }
       catch (SQLException e) {
           con.close();
           statement.close();
           rs.close();
       }
   }

e  public static void main(String[] args) throws Exception {
       try {
           Connection con = getConnection();
           Statement statement = con.createStatement();
           ResultSet rs = statement.executeQuery("SELECT name, email FROM doctor");
       }
       catch (SQLException e) {
           try {
               con.close();
               statement.close();
               rs.close();
           }
           catch (Exception e) {}
       }
   }


b

Note

Option (a) is incorrect because it doesn't care to call close on the database objects con, resultset, and statement.

Option (b) is correct because the try-with-resources statement declares the database resources and closes all of them in reverse order of their creation.

The main method declares to throw the exception Exception so the try-with-resources statement doesn't need to be followed by a catch handler to take care of the exceptions thrown during the creation and auto-closing of the resources.

Options (c) and (d) won't compile.

The reference variables are created in the try-block, so they are out of scope (and thus unknown) in the catch-block.

Option (e) is incorrect.

The code doesn't compile.

The reference variables are created in the try-block, so they are out of scope (and thus unknown) in the catch-block.

The catch handler around the close() method invocations has exactly the same name (e) as its surrounding catch handler.

Also, in the catch handler, the close() methods are invoked in the order Connection, Statement and ResultSet.

On the other hand, the automatic resource closing will call close() in the order ResultSet, Statement, and Connection.




PreviousNext

Related