How to fix: "No suitable driver found for jdbc:mysql://localhost/dbname" error when using pools?
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:
- Click on "Open Launch Config" > Classpath tab set the mysql connector/j jar location. or
- 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.
Related videos on Youtube
Tamer
Updated on March 21, 2020Comments
-
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 almost 13 yearsPlease note that this is definitely not the correct approach to utilize Tomcat's connection pooling facilities.
-
-
Pramod about 12 yearsPrior 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 over 11 yearsI'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 over 11 yearsNo, 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 about 10 yearsYou don't need either statements, for me it worked by just moving the connector to WEB-INF/lib.
-
Lorenzo Dematté over 9 yearsYou 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é over 9 yearsYou 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 over 9 years"should not need" and reality are different =) Have to register driver when running both under Tomcat 7 and 8.
-
Alfishe over 9 yearsNot really a good advice especially if you have multiple applications using different driver versions
-
Max over 9 yearsText 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 almost 9 yearsAccording 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 almost 9 yearsThis 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 over 8 yearsThis helped me alot, but in my case I had to change from
DriverManager.getConnection("jdbc:mysql://localhost:3306;user=username;password=password;database=databasename")
toDriverManager.getConnection("jdbc:mysql://localhost:3306/databasename","user","password")
-
DeveloperDemetri almost 8 yearsTechnically 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 over 7 yearsI'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 about 7 yearsI think, its good practice to register driver before performing operations..
-
Max about 7 yearsGood answer! You might want to explain why the code works :)
-
Hatim almost 7 yearsTHIS IS THE CORRECT ANSWER IN MY CASE
-
iscariot over 6 yearsThis helps me. Thanks a lot!
-
Jones G almost 6 yearsThis solved the issue for me
-
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 over 5 yearsTomcat 8/JDK 1.7: After forced unregistration, Class.forName() doesn't work, registerDriver() does.
-
julianm about 4 yearsMy 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-jdbc/1.3/drizzle-jdbc-1.3.jar && wget http://central.maven.org/maven2/mysql/mysql-connector-java/5.1.27/mysql-connector-java-5.1.27.jar
-
Manoj Gupta over 2 yearsMy 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.