nerdexam
Oracle

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

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 != null) { System.out.print("Connection Established"); } } catch (Exception e) { System.out.print(e); } and the information:
  • The required database driver is configured in the classpath.
  • The appropriate database is accessible with the dbURL, username, and passWord exists.
What is the result?

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)
  • A
    4% (2)
  • B
    13% (7)
  • C
    76% (41)
  • D
    7% (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 ClassNotFoundException would only occur if the JDBC driver class wasn't on the classpath - the question states it is configured.
  • D is wrong because a SQLException would 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.

Full 1Z0-809 Practice