我正在尝试第一次将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;
}
答案 0 :(得分:0)
我的项目中存在相同的问题。我通过在连接URL中使用服务器ip更改localhost来解决它
if (isEmpty(codeEnabledObj)) { // ... }
答案 1 :(得分:0)
确保您的 Mysql 服务器已启动并正在运行:
步骤:
注意:对于windows,您可以通过搜索“services”访问“MySQL80”服务