无法从Java连接到MySQL:MySQL驱动程序连接逻辑中的NullPointerException [英] Can't connect to MySQL from Java: NullPointerException inside MySQL driver connection logic

查看:196
本文介绍了无法从Java连接到MySQL:MySQL驱动程序连接逻辑中的NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试连接到我在Java程序中使用MySQL创建的数据库,但它总是失败。

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能够从IntelliJ中连接到数据库并添加了 mysql-connector-java-5.1.40.jar 添加到项目中,但每次运行程序时 DriverManager.getConnection()抛出这个:

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


推荐答案

这可能是因为您使用的是旧版本的MySQL驱动程序。
您应该尝试使用最新版本。

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

要获得最新版本,您可以检查 https://mvnrepository.com/artifact/mysql/mysql-connector-java

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

截至目前,最新版本为8.0.11。你可以下载它这里或将其添加到 pom.xml

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>



更新



经过进一步调查,它似乎是因为在 MySQL 8.0.1中引入了一个变化


您报告的问题与MySQL
8.0.1中引入的更改有关,即字符集和排序规则支持,现在添加的是'utf8mb4'默认字符集。这些更改突破了
方式Connector / J初始化连接。

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.

如您所知,这已在Connector / J 5.1.41中得到修复,我相信您已确认b $ b已经更新了你的库。

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

参考

如上所述,您问题的另一种解决方法是使用 5.1.41 而不是 5.1.40

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

这篇关于无法从Java连接到MySQL:MySQL驱动程序连接逻辑中的NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆