24

I'm trying to connect to a database I created with MySQL in my Java program, but it always fails.

For the sake of example, here is my code:

import java.sql.*;

public class Squirrel {
    public static void main(String[] args) {
        String user;
        String password;
        Connection connection;
        Statement statement;
        try {
            Class.forName("com.mysql.jdbc.Driver");

            connection = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306", user, password);

            statement = connection.createStatement();

            // Other code
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (statement != null) {
                    statement.close();
                }
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

I am able to connect to the database from within IntelliJ and have added the mysql-connector-java-5.1.40.jar added to the project, but each time I run the program DriverManager.getConnection() throws this:

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:488)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
    at com.mysql.jdbc.Util.getInstance(Util.java:408)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:918)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:897)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:886)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:860)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2330)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2083)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:806)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:488)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:410)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:328)
    at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:678)
    at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:229)
    at Squirrel.main(Squirrel.java:12)
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)
... 13 more
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
J. Willus
  • 537
  • 1
  • 5
  • 15
  • Are you using *try with resources* or somehow closing the connection, then trying to make another call? That’s what I’ve seen cause this error in the past. – achAmháin Apr 28 '18 at 22:20
  • 2
    Wild guess, but could it be because you're not specifying the name of the database in your jdbc url? – TwiN Apr 28 '18 at 22:22
  • Did you set the variables `user` and `password` to the actual username and password before attempting your `getConnection` call? – Kevin Anderson Apr 28 '18 at 22:39
  • @KevinAnderson Yes, I have on my machine. – J. Willus Apr 28 '18 at 22:45
  • @TwiN It still brings up the error. – J. Willus Apr 28 '18 at 22:45
  • 1
    Is there a reason why you're not using the latest version of the mysql jdbc driver? – TwiN Apr 28 '18 at 22:54
  • @TwiN How would go about doing that? I thought I was already using the latest version. – J. Willus Apr 28 '18 at 23:25
  • 1
    @TwiN I've figured out how to use the latest version; IntelliJ automatically chose version `5.1.40` which I then had to manually edit to use the latest version. It now runs without a problem! – J. Willus Apr 29 '18 at 00:26

5 Answers5

45

It might be because you're using an older version of the MySQL driver. You should try using the newest version.

To get the newest version, you can check https://mvnrepository.com/artifact/mysql/mysql-connector-java

As of right now, the newest version is 8.0.11. You can download it here or add this to your pom.xml:

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

Update

Upon further investigation, it seems that it's because of a change that was introduced in MySQL 8.0.1:

The issue you reported is related to the changes introduced in MySQL 8.0.1 wrt the character sets and collations support, with the addition of now being 'utf8mb4' the default char set. Such changes broke the way Connector/J initializes connections.

As you know this was fixed in Connector/J 5.1.41 and I'm sure you already updated your library.

reference

Like mentionned above, an alternative fix to your problem would have been to use the 5.1.41 instead of 5.1.40.

TwiN
  • 3,554
  • 1
  • 20
  • 31
4

Sounds like a potential version mismatch or outdated client. When you run it outside the IDE you may be pulling in the wrong version. I'd make sure the client is on the latest version or similar to the version used by the server.

jspcal
  • 50,847
  • 7
  • 72
  • 76
  • 1
    I've figured it out. The problem was with the outdated `mysql-connector-java-5.1.40.jar`. I was working in Intellij and had to navigate to `File>Project Structure>Libraries>(The J Connector I was using)>Edit` and change the version number to `8.0.11` – J. Willus Apr 29 '18 at 00:31
0

I was importing the wrong version of mysql-connector. Changed the following

@Grab('mysql:mysql-connector-java:5.1.25')

to

@Grab('mysql:mysql-connector-java:5.1.46')

and everything worked as expected.

Dylan
  • 2,161
  • 2
  • 27
  • 51
0

I've tried the above and it doesn't work. Then I checked in my .m2 folder and I noticed that there is a version 5.1.32 and version 8.0.19 mysql connector. However, when I tried to delete the folders, I'm not able to delete version 5.1.32 while the app is running. Obviously it means that version 5 is being in used somehow, eventhough in pom I've specified to use version 8.0.19.

So I just need to invalidate and restart from IntelliJ and voila.

user1885498
  • 823
  • 1
  • 8
  • 19
-2

If you call the IP address 127.0.0.1/localhost then you are communicating with the localhost – in principle, with your own computer.This issue also appears when you don't have localhost configured

For Linux systems

  • Add/Edit "127.0.0.1 localhost" under /etc/hosts if its missing.

For Windows system

  • Add/Edit under C:windows/system32/drivers/etc/hosts if its missing.

For more details on localhost

Gani
  • 422
  • 1
  • 8
  • 16