首页 > 解决方案 > 如何连接到 IntelliJ Ultimate 的 LocalHost MySQL 数据库?

问题描述

我去了数据库 > + > 数据源 > MySQL

这是我的面板的样子: 在此处输入图像描述

这是错误:

[08S01]
    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.
java.net.ConnectException: Connection refused: connect.

这是我的连接文件:

package sample;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DBConnection
{

    private static Connection connection;
    private static final String user = "root";
    private static final String password = "root";
    private static final String database = "jdbc:mysql://localhost:3306/user";

    public static Connection getConnection()
    {
        if (connection == null)
        {
            try
            {
                connection = DriverManager.getConnection(database, user, password);
            } catch (SQLException e)
            {
                e.printStackTrace();
                System.out.println("Could not open database.");
                System.exit(1);

            }
        }
        return connection;
    }

}

我的问题可能是什么?我尝试搜索其他人尝试过的方法,但它们似乎都不起作用,我不得不为我的整个项目制作几份副本,因为我一直在搞砸尝试这些解决方案无济于事。更具体地说,使用 JDBC 和 MySQL 解决“通信链路故障”

标签: javamysqldatabaseintellij-ideaconnection

解决方案


我想您的程序没有运行,因此您在 Intelij 中配置了一些数据源以与您的数据库建立连接。

如果是这样的话

  1. 您的数据库无法访问。检查它是否正在运行,以及此处提供的所有信息是否正确。

  2. 您无需为程序运行设置 InteliJ 数据源。这仅适用于您使用 Intelij 执行 sql 脚本。它与您使用连接到该数据库的 java 开发的某些程序无关。


推荐阅读