nerdexam
Oracle

1Z0-809 · Question #203

Given the code fragment: try { Connection conn = DriverManager.getConnection(dbURL, userName, passWord); String query = "SELECT * FROM Employee WHERE ID = 110"; Statement stat =…

The correct answer is C. The code prints the employee ID. Option C is correct because all JDBC prerequisites are explicitly stated to be satisfied - the driver is loaded, the database is reachable, and the query matches exactly one record. The code correctly establishes a connection, executes the query, iterates the ResultSet with…

Question

Given the code fragment: try { Connection conn = DriverManager.getConnection(dbURL, userName, passWord); String query = "SELECT * FROM Employee WHERE ID = 110"; Statement stat = conn.createStatement(); ResultSet rs = stat.executeQuery(query); while (rs.next()) System.out.print("Employee ID: " + rs.getInt("ID")); } catch (Exception ee) { System.out.println("Error"); } Assume that: The required database driver is configured in the classpath. The appropriate database is accessible with the dbURL, userName, and passWord exists. The Employee table has a column ID of type integer and the SQL query matches one record. What is the result?

Options

  • ACompilation fails at line 14.
  • BCompilation fails at line 15.
  • CThe code prints the employee ID.
  • DThe code prints Error.

How the community answered

(20 responses)
  • A
    5% (1)
  • B
    5% (1)
  • C
    80% (16)
  • D
    10% (2)

Explanation

Option C is correct because all JDBC prerequisites are explicitly stated to be satisfied - the driver is loaded, the database is reachable, and the query matches exactly one record. The code correctly establishes a connection, executes the query, iterates the ResultSet with rs.next(), and calls rs.getInt("ID") on a valid integer column, so it prints the employee ID without incident.

Options A and B (compilation failures) are wrong because the code is syntactically valid - all JDBC methods are called correctly with proper types, and the checked SQLException is handled by catch (Exception ee), since SQLException is a subclass of Exception; the compiler has nothing to object to. Option D is wrong because the catch block only executes when an exception is thrown, but the problem explicitly rules out every condition that would cause one (bad driver, bad URL, bad credentials, missing table/column).

Memory tip: In JDBC questions, "compilation fails" distractors target the fact that JDBC methods throw checked exceptions - but remember, catching the parent Exception class satisfies the compiler for all checked exceptions underneath it, so no compilation error occurs.

Community Discussion

No community discussion yet for this question.

Full 1Z0-809 Practice