1Z0-809 · Question #194
Given the code fragment: try { Properties prop = new Properties(); prop.put("user", userName); prop.put("password", passWord); Connection conn = DriverManager.getConnection(dbURL, prop); if(conn !=…
The correct answer is C. The program prints Connection Established. Option C is correct because all preconditions for a successful JDBC connection are explicitly satisfied: the driver is on the classpath (so it registers automatically via DriverManager), the database is accessible, and the credentials are valid - meaning getConnection() returns…
Question
- The required database driver is configured in the classpath.
- The appropriate database is accessible with the dbURL, username, and passWord exists.
Options
- AA ClassNotFoundException is thrown at runtime.
- BThe program prints nothing.
- CThe program prints Connection Established.
- DA SQLException is thrown at runtime.
How the community answered
(54 responses)- A4% (2)
- B13% (7)
- C76% (41)
- D7% (4)
Explanation
Option C is correct because all preconditions for a successful JDBC connection are explicitly satisfied: the driver is on the classpath (so it registers automatically via DriverManager), the database is accessible, and the credentials are valid - meaning getConnection() returns a non-null Connection object, and the if(conn != null) branch prints "Connection Established".
Why the distractors fail:
- A is wrong because
ClassNotFoundExceptionwould only occur if the JDBC driver class wasn't on the classpath - the question states it is configured. - D is wrong because a
SQLExceptionwould only be thrown if the connection failed (bad URL, wrong credentials, unreachable DB) - again, the question explicitly says the database is accessible and credentials are valid. - B is wrong because with all conditions met, the code reaches
System.out.print("Connection Established")without exception.
Memory tip: Read the setup conditions carefully - exam questions about JDBC often give you all the "happy path" guarantees (driver configured, DB accessible, credentials valid) as a signal that the connection will succeed. If any one of those three is missing or fails, then A, D, or B become the answer instead.
Community Discussion
No community discussion yet for this question.