【发布时间】:2021-06-09 23:57:52
【问题描述】:
我去了数据库 > + > 数据源 > 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;
}
}
我的问题可能是什么?我尝试搜索其他人尝试过的方法,但它们似乎都不起作用,我不得不为我的整个项目制作几份副本,因为我一直在搞砸尝试这些解决方案无济于事。更具体地说Solving a "communications link failure" with JDBC and MySQL
【问题讨论】:
-
您的服务器是否正在运行并监听 3306 端口?使用docs.microsoft.com/en-us/sysinternals/downloads/tcpview 验证。您可以使用任何其他工具连接到此服务器吗?是否有任何防火墙可以阻止连接?
-
@CrazyCoder 假设它没有运行,我该如何让服务器在端口 3306 上运行?
-
@CrazyCoder dang,这两个我都没有做。我遵循了本教程,这就是我认为我必须做的所有事情:youtube.com/watch?v=-yiL99KFeng&t=779s
-
@CrazyCoder 谢谢,我看看并试一试
标签: java mysql database intellij-idea connection