According to this tutorial, calling Class.forName
isn\'t needed anymore with JDBC 4.0+ drivers. I successfully followed the method in the example (just calling
JDBC4 drivers include a file:
META-INF/services/java.sql.Driver
in the jar which uses the ServiceProvider mechanism to register the Driver implementation with the JVM (see javadocs for java.util.ServiceLoader). That's why Class.forName
is no longer necessary.
My guess is that this is a class loader issue. The ServiceLoader
javadoc mentions that:
The provider must be accessible from the same class loader that was initially queried to locate the configuration file; note that this is not necessarily the class loader from which the file was actually loaded.
I would try putting your driver in the tomcat\lib
directory rather than your web app directory to see if that makes a difference (different class loader?).
If you launch your web app through an ide and set a breakpoint, once you hit the breakpoint, you can use the "evaluate expression" feature to execute: ServiceLoader.load(Driver.class)
. This will give you a ServiceLoader
class which you can peek into to see which Drivers are registered. You can check if the mysql driver is there, where in the list it is, etc, which might help in figuring out the behaviour here.