Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 364
Tamer Avatar asked Apr 05 '11 18:04

Tamer


People also ask

How do I fix no suitable driver found for JDBC MySQL?

In brief, we can say that such an error occurs when no JDBC JAR file is added to the classpath of Java. Just we need to add the JAR file to the classpath and then execute the code. The code will hopefully get executed with success.

Could not connect to database no suitable driver found?

SQLException: No suitable driver found for 'jdbc:mysql://localhost:3306/mysql [Solution] This error comes when you are trying to connect to MySQL database from Java program using JDBC but either the JDBC driver for MySQL is not available in the classpath or it is not registered prior to calling the DriverManager.

What JDBC driver used to connect to MySQL?

MySQL JDBC driver To connect to MySQL from Java, you have to use the JDBC driver from MySQL. The MySQL JDBC driver is called MySQL Connector/J. You find the latest MySQL JDBC driver under the following URL: http://dev.mysql.com/downloads/connector/j. The download contains a JAR file which we require later.


1 Answers

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)

like image 147
uncaught_exceptions Avatar answered Sep 28 '22 16:09

uncaught_exceptions