Connecting to MYSQL using Connector/J

296 Views Asked by At

I have installed MySQL Workbench 8.0 and am trying to connect to my database through Netbeans via Connector/J version 5.1.39.

This is a set up that works just fine on my Windows Surface. I'm trying to duplicate the set up on my home PC but am running into issues.

The Code:

String driver       = "jdbc:mysql:";
String hostAndPort  = "//localhost:3306/";
String database     = "bills";
String options      = "?autoReconnect=true&useSSL=false";

final String userName = "root";
final String password = "pass";

String url = driver + hostAndPort + database + options;

    try(Connection con = DriverManager.getConnection(url, userName, password);
        Statement st = con.createStatement())
    {

        // Do nothing so far

    } catch (SQLException ex) {
        System.out.println("SQLException:\t" + ex.getMessage());
        System.out.println("SQLState:\t"     + ex.getSQLState());
        System.out.println("VendorError:\t"  + ex.getErrorCode());
    }

When I try to run the application, it catches the exception and prints:

SQLException:   Could not create connection to database server. Attempted reconnect 3 times. Giving up.
SQLState:       08001
VendorError:    0

Worth mentioning is that yes, the database "bills" exists, the user and password are correct. I can use them to get into MySQL Workbench and edit the bills database.

Things I've tried:

  • I added the Connector/J directory path to the class path.
  • I added the Connector/J jar file directly to the project.
  • I added the "MySQL JDBC Driver" library to the project.
  • I verified the database was set up to use port 3306 with "Standard (TCP/IP)" connection method. (I also have no 'Account Limits' for user root)

enter image description here

  • I verified that Windows Defender Firewall was not blocking MySQL. (I have no other firewall)

enter image description here

  • I verified that the MySQL service was running

enter image description here

Again, this set up works just fine on my Surface. I've tried newer versions of Connector/J but I run into compatibility issues with MySQL Workbench v. 8 so I decided to go with v. 5.1.39 (of Connector/J) since it works on my Surface.

The only place I feel like I am getting a clue as to what is going on is when I check the ports activity on my home pc and compare it to that of my surface:

When observing ports during attempts to connect to the database, I get the following:

enter image description here

Why are port connections not establishing on my Home PC? Is this the source of the problem and how do I fix it?

If this is not the source of the problem, then why can't I connect?

1

There are 1 best solutions below

0
Patrick W On

There is a possibility your JDBC Driver version is incompatible with your MySQL Database Server. You can try updating the driver to a version of your DB in this case Connector/J version 8.0+.