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 DriverManager.getConnection
) for a stand-alone program using MySQL, but when I tried to connect to the exact same database from a class that was part of a web app running on Tomcat 7, it wouldn't work; instead I got a No suitable driver found
exception.
The mysql-connector-java-5.1.18-bin.jar
file was in tomcatwebappsDatabaseProjectWEB-INFlib
, I triple checked, but it wasn't working, so I started trying things. I added a call to Class.forName
and it worked. That was the only thing that changed.
Anyway my question is, does anybody know why this worked or what was going on here? My only theory is that I also have hsqldb.jar
in tomcatlib
for another project and maybe somehow the drivers got confused? But I was under the impression that DriverManager is supposed to be able to tell automatically which driver to use, so that's not supposed to be an issue... Anyway if someone could enlighten me as to what's going on here, I'd really appreciate it.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…