0

I had this message while using MySQL (JDBC driver 5.1.29) with a Java Web Application:

No suitable driver found for jdbc:mysql://localhost/dbname. 

I solved my problem adding Class.forName("com.mysql.jdbc.Driver"); before the connection.

I know since Java 6, Class.forName("com.mysql.jdbc.Driver"); isn't necessary anymore if you're using a recent (JDBC v.4) driver. I can run desktop Java applications without it.

My question is: why I need it with Tomcat 7?

saulotoledo
  • 1,737
  • 3
  • 19
  • 36
  • Works for me without it. The `Class.forName()` is redundant since JDBC 4.0 (as long as the driver complies): nothing specifically to do with Java 6. Upvoted yet another pointless unexplained downvote. – user207421 Feb 10 '14 at 00:07
  • 1
    It's a throwback to an ancient JDBC implementation that required the driver class to be loaded before use. The only benefit (and it's slight) of still doing it is to cause an exception to be thrown on a separate line if the class is not found, instead of having the load exception bubble up out of the JDBC code, where it may get conceptually mixed up with login and connection errors. – Bohemian Feb 10 '14 at 00:44

0 Answers0