1
votes

I am getting an error: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.

EDIT: Link to the Stack Trace

Here is the last "part" of the error, saying it's a NullPointerException:

    at armyofdragons.mule.mysql.Database.<init>(Database.java:28)
    at Main.main(Main.java:6)
Caused by: java.lang.NullPointerException
    at com.mysql.jdbc.ConnectionImpl.getServerCharset(ConnectionImpl.java:2997)
    at com.mysql.jdbc.MysqlIO.sendConnectionAttributes(MysqlIO.java:1934)
    at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1863)
    at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1226)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2253)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2284)
    ... 14 more

`

My URL String: "jdbc:mysql://127.0.0.1:3306/schemaname?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&serverTimezone=GMT"

My Connection code snippet:

connection = null;
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }

        try {
            connection = DriverManager.getConnection(URL, "admin", "passwordcensored123");
        } catch (SQLException e) {
            e.printStackTrace();
        }

I have used the proper schema/database name, username, password, and every other "requirement" needed. I also set the bind-address to 127.0.0.1 and port to 3306. The database is ONLINE and I have made sure that the service is RUNNING.

2
Your connection string isn't exactly localhost, is it? I mean have you tried "jdbc:mysql:localhost"over "jdbc:mysql://127.0.0.1:3306"? The full stacktrace may be helpful too. - Bob Kuhar
That basically is the full stack trace, the error points to the line where it creates the connection @ DriverManager.getConnection(URL, USERNAME, PASSWORD); - ThisIsntSparta
@BobKuhar Doesn't work: java.sql.SQLException: No suitable driver found for jbdc:mysql:localhost at java.sql.DriverManager.getConnection(DriverManager.java:689) at java.sql.DriverManager.getConnection(DriverManager.java:247) at armyofdragons.mule.mysql.Database.<init>(Database.java:29) at Main.main(Main.java:6) - ThisIsntSparta

2 Answers

2
votes

I found out the issue. I was using the wrong version of JDBC / J Connector. A previous StackerOverflow post that I was looking at that provided a solution to adding it to IntelliJ was old and I did not check if it was the up-to-date version or not.

For anyone wanting to know how to add it to IntelliJ, go to Project Structure -> Libraries -> Add (+) -> Add from Maven... -> Enter "mysql:mysql-connector-java:8.0.11" -> Click "Okay".

0
votes

According to https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-usagenotes-connect-drivermanager.html you can use the actual jdbc connection string in DriverManager.getConnection(), have you tried that?

This sort of thing might be very well also a MySql server vs connector version incompatibility issue. Double check your connector version.

Your snippet is a bit tiny, you might want to paste a bit more generously.