No suitable driver found for mysql (Remote debuggi

2019-07-31 23:30发布

问题:

This question already has an answer here:

  • The infamous java.sql.SQLException: No suitable driver found 13 answers

Hi i have found a seemingly common problem, i cant interact with a mysql db. This no suitable driver found keeps happening. I have followed most threads on this same question, however i was not able to replicate their solution so am posting my own specific question.

(Mostly this thread: I can't load the JDBC driver for MySQL)

My code:

public DBManager(){
    try {
        //Class.forName("com.mysql.jdbc.Driver");
        DriverManager.registerDriver(new com.mysql.jdbc.Driver()); 

        con=DriverManager.getConnection("jdbc:mysql:3306//localhost/test","root","root");
        //con=DriverManager.getConnection(c);
        if(!con.isClosed()) {
            con.close();
        }
    }
    catch (SQLException e) {
        e.printStackTrace();
    }

Now, i read that on newer drivers you dont need either Class.forName () or registerDriver(); i have tried both and i still cant get it to work.

The thing is am developing on a windows machine, with eclipse Indigo and Connector/J 5.1.17

and deploying (and debugging) on a remote linux with the same Connector.

Am launching my debug sessions with

java -Xdebug -Xrunjdwp:transport=dt_socket,address=8998,server=y -classpath /home/dev/mysql-connector-java-5.1.17-bin.jar -jar devserver.jar

and i get that exception. Any help?

I come from a heavy c# development environment, so if i have to meddle with classpaths and such please try to step it by step as im not very familiar with that.

回答1:

SQLException: no suitable driver

You will get this exception whenever the DriverManager#getConnection() cannot find a suitable driver for the given connection URL. This in turn happens when the Driver#acceptsURL() has returned false for any of the loaded drivers.

And indeed, your connection URL is wrong:

jdbc:mysql:3306//localhost/test

It should have been:

jdbc:mysql://localhost:3306/test

See also the MySQL JDBC manual.

The JDBC URL format for MySQL Connector/J is as follows, with items in square brackets ([, ]) being optional:

jdbc:mysql://[host][,failoverhost...][:port]/[database] »
[?propertyName1][=propertyValue1][&propertyName2][=propertyValue2]...

If the host name is not specified, it defaults to 127.0.0.1. If the port is not specified, it defaults to 3306, the default port number for MySQL servers.

jdbc:mysql://[host:port],[host:port].../[database] »
[?propertyName1][=propertyValue1][&propertyName2][=propertyValue2]...

If the database is not specified, the connection will be made with no default database.



标签: java mysql jdbc