Question

Here's yet another question about jdbc's mysql driver. Considering the number of search results I got when I googled, I'm pretty bummed nothing I found in them worked for me.

The error:

hostname# java -cp /usr/share/java/mysql-connector.jar:/home/user JDBCTest
java.sql.SQLException: No suitable driver found for jdbc:mysql://<db ip>:3306/dbname
 at java.sql.DriverManager.getConnection(DriverManager.java:596)
 at java.sql.DriverManager.getConnection(DriverManager.java:215)
 at JDBCTest.main(sqltest.java:14)

The code (pulled from a short how to):

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

class JDBCTest {

    private static final String url = "jdbc:mysql://dbipaddress:3306/dbname";

    private static final String user = "username";

    private static final String password = "password";

    public static void main(String args[]) {
        try {
            Connection con = DriverManager.getConnection(url, user, password);
            System.out.println("Success");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

I'm 90% certain /usr/share/java/mysql-connector-java.jar is the correct path for the class. That's what I've found both online, and using locate.

I've tried setting the environment classpath to CLASSPATH=$CLASSPATH:/usr/share/java/mysql-connector-java.jar in /etc/environment. As you can see, I've tried the -cp flag as well.

I can connect to the mysql server and database with the credentials I have in the JDBCTest class using the command line mysql-client. So it is not an error with the db server or my user/password.

As far as I can tell, my jdbc url is correct. That was one of the more common problems I found when searching...

I'm using Ubuntu 12.04 64bit on my servers.

libmysql-java is installed. As is, openjdk-7-jre-headless.

I'm running this completely outside of Tomcat, so all the answers saying to copy the driver into Tomcat's directory shouldn't apply.

So, I'm stumped. I would think using the -cp flag would just force it to work. Is there something in my java install missing? Something that got left out of openjdk-7-jre-headless?

How do I fix this?

Note: This class is just a quick test to help me diagnose why a larger (proprietary) app will not connect to my db. The larger app throws the same error. I'm hoping that fixing this small class will fix the larger app.

Était-ce utile?

La solution

You are probably using a version of the MySQL JDBC driver that is not JDBC 4 compliant, so it is not automatically loaded by DriverManager. In that case you need to explicitly load it using:

Class.forName("com.mysql.jdbc.Driver");

The other option is to use a version of the library that is JDBC 4 compliant and will be automatically loaded.

Autres conseils

Try adding the following on the first line of your main method:

Class.forName("com.mysql.jdbc.Driver");

If it throws an exception, then the JVM cannot access /usr/share/java/mysql-connector.jar. If that is the case, then check file permissions using:

ls -lah /usr/share/java/mysql-connector.jar

You should have at least read access to this file, and obviously the file should exist.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top