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
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)- A5% (1)
- B5% (1)
- C80% (16)
- D10% (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.