How to fix: "No suitable driver found for jdbc:mysql://localhost/dbname" error when using pools? [duplicate] How to fix: "No suitable driver found for jdbc:mysql://localhost/dbname" error when using pools? [duplicate] mysql mysql

How to fix: "No suitable driver found for jdbc:mysql://localhost/dbname" error when using pools? [duplicate]


Try putting the driver jar in the server lib folder. ($CATALINA_HOME/lib)

I believe that the connection pool needs to be set up even before the application is instantiated. (At least that's how it works in Jboss)


The reason you got this error:

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

Is because you forgot to register your mysql jdbc driver with the java application.

This is what you wrote:

Connection con = null;try {    con = DriverManager.getConnection("jdbc:apache:commons:dbcp:test");} catch (SQLException e) {    throw new RuntimeException(e);}

Should be this:

Connection con = null;try {    //registering the jdbc driver here, your string to use     //here depends on what driver you are using.    Class.forName("something.jdbc.driver.YourFubarDriver");       con = DriverManager.getConnection("jdbc:apache:commons:dbcp:test");} catch (SQLException e) {    throw new RuntimeException(e);}

You'll have to read the manual on your specific mysql jdbc driver to find the exact string to place inside the the Class.forName("...") parameter.

Class.forName not required with JDBC v.4

Starting with Java 6, Class.forName("something.jdbc.driver.YourFubarDriver") is not necessary anymore if you use a recent (JDBC v.4) driver. For details read this: http://onjava.com/pub/a/onjava/2006/08/02/jjdbc-4-enhancements-in-java-se-6.html


I had the same problem using Tomcat7 with mysql-connector-java-5.1.26 that I put in both my $CATALINA_HOME/lib and WEB-INF/lib, just in case. But it wouldn't find it until I used either one of these two statements before getting the connection:

DriverManager.registerDriver(new com.mysql.jdbc.Driver ());

OR

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

I then followed up with removing mysql-connector-java-5.1.26 from $CATALINA_HOME/lib and the connection still works.