如何连接到 IntelliJ Ultimate 的 LocalHost MySQL 数据库?

How to connect to IntelliJ Ultimate's LocalHost MySQL database?

我转到数据库 > + > 数据源 > 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;
    }

}

我的问题是什么?我尝试搜索其他人尝试过的方法,但其中 none 似乎有效,我不得不为我的整个项目制作多个副本,因为我一直在尝试这些解决方案而无济于事。更具体地说 Solving a "communications link failure" with JDBC and MySQL

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

如果是这样的话

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

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

您需要有 MySQL 服务器 installed and running 才能从其他应用连接到它。