sb9
sb9

Reputation: 316

JDBC MySQL - Java unable to connect to database server

I have a local MySQL running on the default port, but for some reason my code can't connect to it. restdb is the database name I'm trying to connect to. I can do queries from Workbench, so I'm pretty positive the database itself is up.

I have a maven project, and the mysql-connector (mysql-connector-java-8.0.11.jar) is in my Maven Dependencies. Is this the wrong place?

Here is my code. It fails at getConnection. In my error log it tells me:

Could not create connection to database server. Attempted reconnect 3 times. Giving up.

private Connection getConnection()
{
    Connection conn = null;
    String url = "jdbc:mysql://localhost:3306/restdb?autoReconnect=true&useSSL=true";
    String username = "root";
    String password = "password";
    try
    {           
        DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
        conn = DriverManager.getConnection(url, username, password);
    } catch (Exception e)
    {
        System.err.println(e.getMessage());
    }
    return conn;
}

Upvotes: 0

Views: 445

Answers (1)

satsie
satsie

Reputation: 399

It looks like you don't have SSL setup. Unless this is intentional, remove the 'useSSL=true' part of the URL.

Upvotes: 1

Related Questions