[Solved] java.sql.SQLException: No suitable driver found for localhost test [duplicate]

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.mysql.client.ConnDb;

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

       con = DriverManager.getConnection(host+db, user, pass);
       return "Connected to Database";
     } catch(Exception ex) {
         return ex.toString();

Solution #1:

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:

Respondent: BalusC

Solution #2:

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.

Respondent: Richard

Solution #3:

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

Respondent: S M Slack

Solution #4:

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

Respondent: SkyTreasure

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Most Popular

To Top
India and Pakistan’s steroid-soaked rhetoric over Kashmir will come back to haunt them both clenbuterol australia bossier man pleads guilty for leadership role in anabolic steriod distribution conspiracy