【问题标题】:The driver has not received any packets from the server in IntelliJ IDEA [duplicate]驱动程序没有从 IntelliJ IDEA 中的服务器收到任何数据包 [重复]
【发布时间】:2019-06-14 19:28:41
【问题描述】:

我第一次尝试将我的 intelliJ java 项目与 mysql 连接,不幸的是我收到了这个错误:

com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:835)
at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:455)
at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:240)
at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:207)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:677)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:228)
at sundos.ConnectionConfig.getConnect(ConnectionConfig.java:15)
at sundos.Main.main(Main.java:15)
Caused by: com.mysql.cj.exceptions.CJCommunicationsException: 
Communications link failure

我已经添加了 mysql-connector。 这是我的一个简单代码:

public static Connection getConnect (){

    Connection connection = null;

    try {

        Class.forName("com.mysql.cj.jdbc.Driver");
        connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test" , "root" , "12345");
    } catch (Exception e){
        e.printStackTrace();
    }

    return connection;
}

【问题讨论】:

    标签: java mysql jdbc


    【解决方案1】:

    我在我的项目中遇到了同样的问题。我通过在连接 url 中使用服务器 ip 更改 localhost 来解决它

     connection = DriverManager.getConnection("jdbc:mysql://192.168.3.3:3306/test" , "root" , "12345");
    

    【讨论】:

      【解决方案2】:

      确保您的 Mysql server 已启动并运行:

      步骤:

      1. 检查MySQL80服务是否正在运行
      2. 尝试使用mysqlWorkbench 或任何其他工具连接到您的数据库
      3. 重新运行您的应用

      注意:对于Windows,您可以通过搜索services访问MySQL80服务

      【讨论】:

        猜你喜欢
        • 2021-05-22
        • 2020-09-18
        • 2011-01-11
        • 1970-01-01
        • 2017-08-23
        • 2015-08-28
        • 1970-01-01
        • 2014-06-05
        • 2018-10-02
        相关资源
        最近更新 更多