Ross
Ross

Reputation: 46987

DriverManager.getConnection() returns null, rather than a connection object

I am trying to connect to an Oracle database via JDBC. Using the following code:

Connection c = null;
try {
     Class.forName("oracle.jdbc.driver.OracleDriver");
     c = DriverManager.getConnection(connURL, userID, password);
 } catch (SQLException se) {
     System.out.println(se.getMessage());
 } catch (Exception e) {
     System.out.println(e.getMessage());
 }

For some reason no exception is thrown but c remains null - what does this mean?

Update:

Turns out we were getting an exception - Class not found: "oracle.jdbc.driver.OracleDriver" - we had the odbc classes outside the classpath.

Upvotes: 0

Views: 6025

Answers (1)

Isaac Truett
Isaac Truett

Reputation: 8884

This would have been easier to spot if you handled your exceptions differently. Just printing the exception message and moving on is rarely the right thing to do. What can you do when the database connection is null? If you throw an exception indicating that the connection isn't available, then whatever routine is trying to get a connection to the database can alert the user to a potential system outage, log the error, and email a system administrator (for example). Just returning null is less obvious to troubleshoot, as you have found.

Upvotes: 1

Related Questions