无法从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.

相关文章

优化MySQL数据库发布系统存储的方法有:1.mysql库主从读写分...
使用mysql的方法:在“我的电脑”→右键→“管理”→“服务”...
在mysql中查看root用户权限的方法:1.命令行启动mysql服务;...
MySQL主从复制是用来备份一个与主数据库一样环境的从数据库,...
运行mysql的方法1.启动mysql服务,在“我的电脑”→右键→“...
开启mysql的方法1.可以通过快捷键win+r,输入cmd,打开窗口,...