加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 数据库 > MySql > 正文

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

发布时间:2020-05-27 13:18:39 所属栏目:MySql 来源:互联网
导读:我正在尝试连接到我在Java程序中使用MySQL创建的数据库,但它总是失败.为了举例,这是我的代码:import java.sql.*; public class Squirrel { public static void main(String[] args) { String user; String passw

我正在尝试连接到我在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驱动程序.
您应该尝试使用最新版本.

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

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


更新

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

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读