无法从 Java 连接到 MySQL:MySQL 驱动程序连接逻辑中的 NullPointerException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50081527/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 23:25:17  来源:igfitidea点击:

Can't connect to MySQL from Java: NullPointerException inside MySQL driver connection logic

javamysql

提问by J. Willus

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

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

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.jaradded to the project, but each time I run the program DriverManager.getConnection()throws this:

我能够从 IntelliJ 中连接到数据库并将添加的mysql-connector-java-5.1.40.jar添加到项目中,但是每次运行程序时都会DriverManager.getConnection()抛出:

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

采纳答案by TwiN

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

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

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

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

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

截至目前,最新版本是 8.0.11。您可以在此处下载或将其添加到您的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:

经过进一步调查,似乎是因为在 中引入了更改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.

您报告的问题与 MySQL 8.0.1 中引入的更改有关,其中包含字符集和排序规则支持,现在添加了默认字符集“utf8mb4”。此类更改破坏了 Connector/J 初始化连接的方式。

如您所知,此问题已在 Connector/J 5.1.41 中修复,我确定您已经更新了您的库。

reference

参考

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

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

回答by jspcal

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.

听起来像是潜在的版本不匹配或过时的客户端。当您在 IDE 之外运行它时,您可能会引入错误的版本。我会确保客户端使用最新版本或与服务器使用的版本相似。

回答by Gani

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 localhostconfigured

如果您调用 IP 地址 127.0.0.1/localhost 那么您正在与 localhost 通信——原则上,与您自己的计算机进行通信。当您没有配置localhost时也会出现此问题

For Linux systems

对于 Linux 系统

  • Add/Edit "127.0.0.1 localhost" under /etc/hosts if its missing.
  • 如果 /etc/hosts 丢失,请在 /etc/hosts 下添加/编辑“127.0.0.1 localhost”。

For Windows system

对于 Windows 系统

  • Add/Edit under C:windows/system32/drivers/etc/hosts if its missing.
  • 如果缺少,则在 C:windows/system32/drivers/etc/hosts 下添加/编辑。

For more details on localhost

有关本地主机的更多详细信息