0

I'm having issues connecting to an external IP address using JDBC. When I execute the following code, I get this error No suitable driver found for jdbc:mysql://52.206.157.109:3306/U054Jk

Code:

package util;

import java.sql.*;

public class db {
    private static String server = "52.206.157.109";
    private static String dbName = "U054Jk";
    private static String userName = "secret";
    private static String password = "secret";

    private static Connection getCon() throws SQLException {
        String host = "jdbc:mysql://" + server + ":3306/" + dbName;

        Connection conn = DriverManager.getConnection(
                host,
                userName,
                password
        );

        return conn;
    }

    public static ResultSet ExecQuery(String query) throws SQLException {
        //Get the connection
        Connection conn = getCon();

        //Create the statement
        Statement stmt = conn.createStatement();

        //Execute the statement
        ResultSet rs = stmt.executeQuery(query);

        //Return ResultSet
        return rs;
    }
}

I can connect with my SQL client just fine using the credentials but can't quite figure out the JDBC string that I need for the URL. Thank you for your help.

Desquid
  • 131
  • 2
  • 12

1 Answers1

3

Please add MySQL Connector in the classpath. Your project needs a JDBC driver implementing the interfaces of JDBC.

If you are using Apache Maven, add the following in the pom.

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.13</version>
</dependency>

Or else, download the jar from the link and add it to the classpath.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Thiru
  • 2,541
  • 4
  • 25
  • 39