0

When I try to run this code, I get a no suitable driver error. How would I fix this problem?

public class DBConnect {

    public static void main(String[] args)
    {
        String url = "jdbc:mysql://***.***.***.**:3306/*******";
        String user = "root";
        String password = "root";

        try {
            Connection con = DriverManager.getConnection(url, user, password);
            System.out.print("Connected");
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println( e.getMessage( ) );
        }
    }
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Kirby127
  • 39
  • 1
  • 1
  • 10
  • I don't see where the driver was loaded. Make sure the MySQL connector JAR is in your CLASSPATH, the driver class is loaded and registered, and your connection URL syntax is correct. That exception usually means a mismatch between driver class and URL syntax. – duffymo May 15 '17 at 11:56

1 Answers1

0

Include this line prior to creating the connection (and make sure the relevant JAR containing the MySQL driver is on your classpath).

Class.forName("com.mysql.jdbc.Driver").newInstance();
Riaan Nel
  • 2,425
  • 11
  • 18