微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

为了举例,这是我的代码

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();
            }
        }
    }
}

我能够从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.MysqL.jdbc.JDBC4Connection.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驱动程序.
您应该尝试使用最新版本.

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

截至目前,最新版本为8.0.11.您可以下载它here或将其添加到您的pom.xml:

MysqL

更新

经过进一步调查,似乎是因为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

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

原文地址:https://www.jb51.cc/mysql/433304.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐