Java OCA OCP Practice Question 2492

Question

Which of the following options will populate ResultSet rs with all rows from table Contact, assuming getConnection() returns a valid Connection object?

a  public static void main(String[] args) throws Exception{
       Connection con = getConnection();
       Statement statement = con.createStatement();
       ResultSet rs = statement.executeQuery("SELECT * FROM contact");
   }/* w  w w .java 2 s  .  c om*/

b  public static void main(String[] args) {
       Connection con = getConnection();
       Statement statement = con.createStatement();
       ResultSet rs = statement.executeQuery("SELECT * FROM contact");
   }

c  public static void main(String[] args) throws SQLException{
       Connection con = getConnection();
       Statement statement = con.prepareStatement();
       ResultSet rs = statement.executeQuery("SELECT * FROM contact");
   }

d  public static void main(String [] args) throws Throwable{
       Connection con = getConnection();
       Statement statement = con.callableStatement("SELECT * FROM contact");
       ResultSet rs = statement.executeUpdate();
   }


a

Note

The code in option (b) won't compile.

This code neither handles the checked SQLException thrown by the JDBC API statements, nor declares the main method to throw it.

Option (c) is incorrect because you must pass the SQL query when you create an object of PreparedStatement using Connection's method prepareStatement().

Option (d) is incorrect because class Connection doesn't define method callableStatement().

The correct method to create an object of CallableStatement is prepareCall() (from class Connection).




PreviousNext

Related