1

Im trying to connect my intelliJ java project with mysql for the first time and unfortunately I get this error :

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

I already added the mysql-connector. here is a simple code of me :

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;
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Sundos Altamimi
  • 29
  • 1
  • 1
  • 12

2 Answers2

0

I have a same problem in my project. I solved It by changing localhost with server ip in connection url

 connection = DriverManager.getConnection("jdbc:mysql://192.168.3.3:3306/test" , "root" , "12345");

Also make sure that your mysql server is running and your username and password are correct.

0

make sure your Mysql server is up and running:

steps:

  1. check if MySQL80 service is running
  2. try connect to your database using mysqlWorkbench or any other tool
  3. rerun your app

Note: for windows you can access MySQL80 service by searching for services

Saeed Masoomi
  • 1,703
  • 1
  • 19
  • 33
MonirRouissi
  • 549
  • 8
  • 7