1

I have a class A which connects to mysql database in my localhost. It works fine in eclipse.
Then I modified the project to call/invoke class A from a servlet and deployed to tomcat.
I am getting this exception:

No suitable driver found for jdbc:mysql://localhost:3306/a

and this exception occurs at:

DriverManager.getConnection(connectionUrl, user, password);

What is going wrong here? And how can I fix this problem?

Add:
I'm sure the .jar is under WEB-INF/lib, and this is not the problem.
I just figured it out and the problem is that I forgot to add this line:

Class.forName("com.mysql.jdbc.Driver");

Is there anybody knows that why this line is a must in a web application and not in a java project?

Jens
  • 60,806
  • 15
  • 81
  • 95
safarisoul
  • 247
  • 1
  • 5
  • 12

1 Answers1

4

No suitable driver" usually means that the JDBC URL you've supplied to connect has incorrect syntax or when the driver isn't loaded at all.

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.(using Class.forName())

Also check that you have mysql-jdbc.jar in your classpath. I would suggest to place .jar at physical location to /WEB-INF/lib directory of your project.Then tomcat will take care for the rest.

Hardik Mishra
  • 14,171
  • 9
  • 58
  • 94