12

When trying to connect to mysql I always get this error:

java.sql.SQLException: No suitable driver found for localhost test

I already included the mysql-connector.jar in the /WEB-INF/lib in my app. What else do I need to configure to make it work? Do I need to add something in web.xml? I'm not using the appengine.

Here is my code in the server:

package com.mysql.server;

import java.sql.Connection; 
import java.sql.DriverManager;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.mysql.client.ConnDb;

public class ConnDbImpl extends RemoteServiceServlet implements ConnDb {
   public Connection con;
   @Override
    public String tryConn() {
     try{
       String host = "localhost";
       String db = "test";
       String driver = "com.mysql.jdbc.Driver";
       String user = "root";
       String pass = "pwd";

       Class.forName(driver).newInstance();
       con = DriverManager.getConnection(host+db, user, pass);
       return "Connected to Database";
     } catch(Exception ex) {
         return ex.toString();
     }    
   } 
}
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
user1035079
  • 151
  • 1
  • 1
  • 7

4 Answers4

26

You will get this exception when the JDBC URL is not accepted by any of the loaded JDBC drivers as per the Driver#acceptsURL() method. You actually forgot the JDBC driver specific URI prefix. For the MySQL JDBC driver this is jdbc:mysql://. The full connection URL should look like this:

con = DriverManager.getConnection("jdbc:mysql://localhost/test", user, pass);

See also:

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
6

I found another cause for this error message. In my case the user simply had no privilege to the database e.g. to the selected table. Dear driver developers, why do you use such misleading error messages? A lot of people have real trouble with this.

Richard
  • 61
  • 1
  • 1
  • Thanks for that, Had I not kept reading to your response, I probably would have walked down a day-or-two long rabbit hole trying to figure out how to fix my server install. What an odd error message! – daemonl Jun 12 '13 at 04:23
0

For me, it was forgetting to include the MySQLJDBC Driver in the project libraries. DOH!

0

This was giving that error:

Connection connection= DriverManager.getConnection("jdbc:mysql://localhost:3306/lib_db","root","root");

but when I changed that to:

Connection connection =DriverManager.getConnection("jdbc:mysql://localhost/db_name?"+"user=root&password=root");

error was gone

Khalil M
  • 1,432
  • 2
  • 17
  • 33
SkyTreasure
  • 755
  • 1
  • 9
  • 20