0

I am getting a java.sql.SQLException: No suitable driver found. I am trying to connect to a remote database. I think I have all my credentials correct.

Here is my code

public static void main(String[] args) {
    String dbUrl = "jdbc:mysql:sshhost/db1";
    try {
        Connection myConn = DriverManager.getConnection(dbUrl, "usr", "");
        Statement myStat = myConn.createStatement();
        ResultSet myRs = myStat.executeQuery("select * from students");
        while (myRs.next()) {
            System.out.println(myRs.getString("last_name") + ", " + myRs.getString("first_name"));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

The host is the same remote host that I ssh into. I log on to mysql as root and this requires no password so I left the last parameter "" The database I want to use while logged into root is called db1. I'm not sure if my parameters are valid.

5
  • 3
    I think you're missing the mysql connector/j jar on your classpath; that's what the no suitable driver message means. Oct 13, 2014 at 23:05
  • Keep in mind that the database username and password aren't necessarily the same as your Linux password.
    – Tim
    Oct 13, 2014 at 23:39
  • @Tim I am changing things around but not I get com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Access denied for user 'username'@'%' to database 'db1' at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:526) I think I am getting confused to what are the correct credentials... it's driving me crazy
    – Liondancer
    Oct 13, 2014 at 23:53
  • Figure out your database credentials first, by connecting (outside of your program) using something like SQLPlus. Once you know them, it should be simple to configure JDBC to use them.
    – Tim
    Oct 14, 2014 at 0:50
  • Also, your new error message implies that you got past the "No suitable driver found" exception; what did you change to do that?
    – Tim
    Oct 14, 2014 at 0:51

2 Answers 2

2

You have not loaded a driver. JDBC need to have a driver loaded before a connection can be made. So make sure you load an apt driver class using:

Class.forName("driverClass");

If you are using Java 6 or above(which included JDBC 4), you don't need the above mentioned dynamic driver class loading.

In both the cases, make sure the driver class you are using is present in your classpath.

4
  • I'm new to this JDBC sorry. I'm still not quite sure what this means. I have added a dependency for connector/j in my pom.xml but that is about it
    – Liondancer
    Oct 13, 2014 at 23:09
  • 1
    I thought that in JDBC 4.0, calling Class.forName() was no longer necessary. Source
    – Tim
    Oct 13, 2014 at 23:30
  • @Tim Yes you are right, post Java 6, which includes JDBC 4 ,does not require explicit driver loading. Updated the answer, thanks! Oct 13, 2014 at 23:33
  • Note, it requires the driver impl to support the auto-loading. so simply using java 6+ isn't enough. i would imagine most modern drivers are updated, but you never know...
    – jtahlborn
    Oct 13, 2014 at 23:34
0

You'll need to update your classpath to point to the mysql jar file. The JVM can't find a suitable mysql driver.

2
  • I currently have connector/j in my pom.xml. I thought that was enough but I guess I am wrong.` <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.6</version> </dependency>`
    – Liondancer
    Oct 13, 2014 at 23:07
  • Driver is not loaded using dynamic class loading. It is required before a connection can be established. Oct 13, 2014 at 23:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.