Reputation: 577
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
Upvotes: 28
Views: 27091
Reputation: 843
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.
Upvotes: 0
Reputation: 2219
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.
Upvotes: 1
Reputation: 442
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
For Windows system
Upvotes: -2
Reputation: 3834
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>
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.
Like mentionned above, an alternative fix to your problem would have been to use the 5.1.41
instead of 5.1.40
.
Upvotes: 50
Reputation: 51924
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.
Upvotes: 6