I've been messing with this for hours now and could really use some assistance. Here's the skinny:
Postgres ver: 9.1.9, Tomcat version 7.0.26, and Java version 1.7.0_25, OS Ubuntu 12.04 - I'm trying to connect to the database using JDBC version 4. The .jar file is located in $CATALINA_HOME/lib. I receive the "No suitable driver found..." message in the logs and of course this: http://pastebin.com/wCec85bN
My dev environment is Windows 7, Tomcat 7.0.12, same JDBC driver, and Java 1.7.0.
The path to the JDBC driver is in /etc/environments in the CLASSPATH.
From everything I've read I don't need to use Class.forName(org.postgresql.Driver) with JDBC 4. I've tried it anyway and always get a "class not found exception" error.
The JDBC jar file in $CATALINA_HOME/lib is the only one not linked, I notice. I wonder if this has something to do with it (not being linked)?
Works great in my Dev environment; not so much on my VPS
First of all I'd suggest you to include your JDBC jars in WEB-INF lib directory... I'm not a Tomcat expert but as far as I know it's better avoiding to add specific jar libraries to a Java webserver general classpath.
What happens if you try to register your Driver using DriverManager.registerDriver method ? Does it work or not ?
K.C. Brawley wrote:From everything I've read I don't need to use Class.forName(org.postgresql.Driver) with JDBC 4.
That's a myth. JDBC allows the ServiceLoader mechanism to be used, but only if the driver JAR file support it. The PostgreSQL driver does however.
That said, the following from the DriverManager Javadoc is very important (emphasis added by me):
When the method getConnection is called, the DriverManager will attempt to locate a suitable driver from amongst those loaded at initialization and those loaded explicitly using the same classloader as the current applet or application.
From what I could see in the Java 7 source code, the initialization uses the java.sql.Driver class' class loader. In other words, I wouldn't trust the ServiceLoader mechanism if the JAR file is loaded by any other class loader.