Home > database >  connection pool is returning null
connection pool is returning null

Time:07-21

I am new to Java, and trying to see why does the

 private static PoolConnectionManager pcm = null;
 con = pcm.getConnection();
 ps = con.prepareStatement("SELECT name FROM employee");

returns null, whereas when I set

 Class.forName("oracle.jdbc.driver.OracleDriver");      
 con = DriverManager.getConnection(url, username, password);

connection seem to work fine.

what am I doing wrong with the connection pool thats messing the connection?

CodePudding user response:

The variable pcm is initialized as null in your code. null is a non-existent value, meaning your PoolConnectionManager is non-existent. You need to initialize your PoolConnectionManager with an actual PoolConnectionManager instance in order to use any of its methods. You would usually do this using the new keyword, for example private static PoolConnectionManager pcm = new PoolConnectionManager();, filling in the constructor parameters as necessary.

The reason the second code block works is because DriverManager has a static method called getConnection. A static method can be accessed from the Class object itself, without having to create an instance of that class, which is what you're doing here. The first code line doesn't actually do anything.

  • Related