java - 如何连接到 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 解决“通信链路故障”
解决方案
我想您的程序没有运行,因此您在 Intelij 中配置了一些数据源以与您的数据库建立连接。
如果是这样的话
您的数据库无法访问。检查它是否正在运行,以及此处提供的所有信息是否正确。
您无需为程序运行设置 InteliJ 数据源。这仅适用于您使用 Intelij 执行 sql 脚本。它与您使用连接到该数据库的 java 开发的某些程序无关。
推荐阅读
- jquery - 如何在 SQL 中查询此查询?
- python-3.x - 使用条件格式 pd.xlsxwriter 比较两个日期
- c# - 在 C# 中将 JSON 数据反序列化为列表
- eclipse - 如何在启动 tomcat 时修复错误“无法初始化连接器 [连接器 [HTTP/1.1-443]]”
- javascript - 模态中的ng-click未调用组件中的函数
- android - Kotlin ViewModel List of String
- angular7 - 从 mongodb 映射文档值的问题
- python - “子流程”模块的问题
- r - 如何将R中的表格分成两部分
- spring - Spring boot 2.1.3 从 2.0.6 更新导致 util:map 的 BeanDefinitionParsingException