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

551,455

Solution 1

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)

Solution 2

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

Solution 3

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.

Solution 4

When running tomcat out of eclipse it won't pick the lib set in CATALINA_HOME/lib, there are two ways to fix it. Double click on Tomcat server in eclipse servers view, it will open the tomcat plugin config, then either:

  1. Click on "Open Launch Config" > Classpath tab set the mysql connector/j jar location. or
  2. Server Location > select option which says "Use Tomcat installation (take control of Tomcat installation)"

Solution 5

I had the mysql jdbc library in both $CATALINA_HOME/lib and WEB-INF/lib, still i got this error . I needed Class.forName("com.mysql.jdbc.Driver"); to make it work.

Share:
551,455

Related videos on Youtube

Tamer
Author by

Tamer

Updated on March 21, 2020

Comments

  • Tamer
    Tamer almost 4 years

    I am trying to create a connection to my database, when I put test my code using the main method, it works seamlessly. However, when trying to access it through Tomcat 7, it fails with error:

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

    I am using pooling. I put in mysql connector (5.1.15), dbcp (1.4) , and pool(1.4.5) libraries in WEB-INF/lib and in .classpath as well. I am using Eclipse IDE. My code for the database driver is:

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    
    import org.apache.tomcat.dbcp.dbcp.ConnectionFactory;
    import org.apache.tomcat.dbcp.dbcp.DriverManagerConnectionFactory;
    import org.apache.tomcat.dbcp.dbcp.PoolableConnectionFactory;
    import org.apache.tomcat.dbcp.dbcp.PoolingDriver;
    import org.apache.tomcat.dbcp.pool.impl.GenericObjectPool;
    
    public class DatabaseConnector {
        public static String DB_URI = "jdbc:mysql://localhost/dbname";
        public static String DB_USER = "test";
        public static String DB_PASS = "password";
    
        // Singleton instance
        protected static DatabaseConnector _instance;
    
        protected String _uri;
        protected String _username;
        protected String _password;
    
        /**
         * Singleton, so no public constructor
         */
        protected DatabaseConnector(String uri, String username, String password) {
            _uri = uri;
            _username = username;
            _password = password;
    
            GenericObjectPool connectionPool = new GenericObjectPool(null);
            ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(
                _uri, _username, _password);
            PoolableConnectionFactory poolableConnectionFactory =
                new PoolableConnectionFactory(connectionFactory, connectionPool,
                                                null, null, false, true);
            PoolingDriver driver = new PoolingDriver();
            driver.registerPool("test", connectionPool);
        }
    
        /**
         * Returns the singleton instance
         */
        public static DatabaseConnector getInstance() {
            if (_instance == null) {
                _instance = new DatabaseConnector(DB_URI, DB_USER, DB_PASS);
            }
            return _instance;
        }
    
        /**
         * Returns a connection to the database
         */
        public Connection getConnection() {
            Connection con = null;
            try {
                con = DriverManager.getConnection("jdbc:apache:commons:dbcp:test");
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            return con;
        }
    }
    

    Start of my stack trace:

    Apr 5, 2011 9:49:14 PM org.apache.catalina.core.StandardWrapperValve invoke
    SEVERE: Servlet.service() for servlet [Login] in context with path [/Project] 
    threw exception
    java.lang.RuntimeException: java.sql.SQLException: 
    No suitable driver found for jdbc:mysql://localhost/dbname
    

    What is causing this error?

    • BalusC
      BalusC almost 13 years
      Please note that this is definitely not the correct approach to utilize Tomcat's connection pooling facilities.
  • Pramod
    Pramod about 12 years
    Prior to the release of the JDBC 4.0 specification, the client application needed to load the Driver class via the command Class.forName("..."). I even checked META-INF/services/java.sql.Driver file. In my case application runs fine in jetty server (via maven or stand alone), but when I try to run it in other servers (I tested in tomcat and glassfish) I get above error. I even tried to copy mysql-connector-java-5.1.18.jar inside apache-tomcat-7.0.23/lib, so what might be the cause of it not running in these servers.
  • Brad Whitaker
    Brad Whitaker over 11 years
    I've found that with Tomcat 7 and Java 7 it is once again necessary to execute Class.forName("something.jdbc.driver.YourFubarDriver"); At least this solved this problem for me on one deployment. The same .war worked fine on Tomcat 5.5 with Java 6, but threw the no suitable driver exception on 7/7.
  • Pino
    Pino over 11 years
    No, starting with Java 6, Class.forName("something.jdbc.driver.YourFubarDriver") is not necessary anymore if you use a recent (JDBC v.4) driver. Probably with Tomcat 7 you where using an older driver than with Tomcat 5.5. See Javadoc of DriverManager for details.
  • Vlad Schnakovszki
    Vlad Schnakovszki about 10 years
    You don't need either statements, for me it worked by just moving the connector to WEB-INF/lib.
  • Lorenzo Dematté
    Lorenzo Dematté over 9 years
    You should not need them. However, it is highly dependent on the driver version/OS version/Tomcat version/Java version. Even "recent" driver and JDK combinations may fail. To be on the safe side, you need to use Class.forName
  • Lorenzo Dematté
    Lorenzo Dematté over 9 years
    You should not need them. However, it is highly dependent on the driver version/OS version/Tomcat version/Java version. Even "recent" driver and JDK combinations may fail. To be on the safe side, you need to use Class.forName
  • Alfishe
    Alfishe over 9 years
    "should not need" and reality are different =) Have to register driver when running both under Tomcat 7 and 8.
  • Alfishe
    Alfishe over 9 years
    Not really a good advice especially if you have multiple applications using different driver versions
  • Max
    Max over 9 years
    Text copied from this answer: stackoverflow.com/a/5484287/2479087 ; Never call DriverManager.registerDriver() method manually. The JDBC spec requires a driver to register itself when the class is loaded, and the class is loaded via Class.forName(). In JDBC 4 the drivers are able to be loaded automatically just by being on the class path.
  • Shannon
    Shannon almost 9 years
    According to tomcat.apache.org/tomcat-8.0-doc/… "web applications that have database drivers in their WEB-INF/lib directory cannot rely on the service provider mechanism and should register the drivers explicitly."
  • Litera
    Litera almost 9 years
    This also helped me when I was setting up TeamCity. Copied jar from the Platform Independent zip file into the folder prompted in TeamCity setup and hit refresh drivers button.
  • desw
    desw over 8 years
    This helped me alot, but in my case I had to change from DriverManager.getConnection("jdbc:mysql://localhost:3306;use‌​r=username;password=‌​password;database=da‌​tabasename") to DriverManager.getConnection("jdbc:mysql://localhost:3306/dat‌​abasename","user","p‌​assword")
  • DeveloperDemetri
    DeveloperDemetri almost 8 years
    Technically it is not required with JDBC 4+... but the project I am working on requires it to load the Postgres JDBC. It was working fine in a small standalone project, but for some reason the Class.forName(...) was needed in the main web project. Weird stuff.
  • DavidS
    DavidS over 7 years
    I'm confused, why was it ever necessary to call Class.forName? Shouldn't this class be on the classpath? Why do we need to "load" a class manually? I've never needed to manually load a class, what's special about this one?
  • peaceUser
    peaceUser about 7 years
    I think, its good practice to register driver before performing operations..
  • Max
    Max about 7 years
    Good answer! You might want to explain why the code works :)
  • Hatim
    Hatim almost 7 years
    THIS IS THE CORRECT ANSWER IN MY CASE
  • iscariot
    iscariot over 6 years
    This helps me. Thanks a lot!
  • Jones G
    Jones G almost 6 years
    This solved the issue for me
  • Martin
    Martin over 5 years
    @DavidS Loading a JDBC driver class causes the class to register itself at the DriverManager as required by the JDBC specification, see stackoverflow.com/q/32922459 and stackoverflow.com/q/5484227.
  • access_granted
    access_granted over 5 years
    Tomcat 8/JDK 1.7: After forced unregistration, Class.forName() doesn't work, registerDriver() does.
  • julianm
    julianm about 4 years
    My enviroment is running in docker, so I added this instruction to Dockerfile: RUN cd /usr/local/tomcat/lib && wget http://central.maven.org/maven2/org/drizzle/jdbc/drizzle-jdb‌​c/1.3/drizzle-jdbc-1‌​.3.jar && wget http://central.maven.org/maven2/mysql/mysql-connector-java/5‌​.1.27/mysql-connecto‌​r-java-5.1.27.jar
  • Manoj Gupta
    Manoj Gupta over 2 years
    My tomcat server is running using XAMPP tool so I have added jar file into following path: "C:\xampp\tomcat\lib" and it is working file for me.

Related