Liondancer
Liondancer

Reputation: 16469

connecting to remote database with jdbc, verify credentials

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.

Upvotes: 0

Views: 126

Answers (2)

Juned Ahsan
Juned Ahsan

Reputation: 68715

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.

Upvotes: 2

Michael Krause
Michael Krause

Reputation: 4849

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

Upvotes: 0

Related Questions