I wrote some code that connects to a PostgreSQL 9.4 DB via its latest JDBC4 driver. My understanding is that JDBC4 no longer needs the boilerplate "Class.forName("org.postgresql.Driver") driver registration line. Thus, I left it out. It worked fine.
I then put the same exact code in a servlet, added the postgreSQL JDBC jar to WebContent/WEB-INF/lib and it failed saying:
java.sql.SQLException: No suitable driver found for jdbc:postgresql://localhost/
When I add the Class.forName line, it then works fine.
Does anyone know why this JDBC4 driver needs Class.forName when put within a doGet() servlet method, but in a basic Java class it doesn't?
Thanks, TR
edit: Forgot to mention, I'm using Java 7.
Some updates:
It does appear that this is a fairly common issue in Tomcat. I've moved the JAR out of WEB-INF/lib per the suggestions from this, and other posts. Even with it in TOMCAT_HOME/lib I need to use Class.forName(). Sort of odd, I suppose it has something to do with the way Tomcat registers classes.
Found this post which is the exact issue with the mySQL JDBC driver rather than postgreSQL:
When is Class.forName needed when connecting to a database via JDBC in a web app?