Communications link failure: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException

+1 vote

I'm working on getting my database to talk to my Java programs.

Can someone give me a quick and dirty sample program using the JDBC?

Contents of the test file:

import com.mysql.jdbc.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class SqlTest {

    public static void main(String [] args) throws Exception {
        // Class.forName( "com.mysql.jdbc.Driver" ); // do this in init
        // // edit the jdbc url 
        Connection conn = DriverManager.getConnection( 
            "jdbc:mysql://localhost:3306/projects?user=user1&password=123");
        // Statement st = conn.createStatement();
        // ResultSet rs = st.executeQuery( "select * from table" );

        System.out.println("Connected?");
    }
}

I'm getting a rather stupendous error:

Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.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 sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1122)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2260)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:787)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:49)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:357)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)
    at java.sql.DriverManager.getConnection(DriverManager.java:582)
    at java.sql.DriverManager.getConnection(DriverManager.java:207)
    at SqlTest.main(SqlTest.java:22)
Caused by: com.mysql.jdbc.exceptions.jdbc4.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 sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method).......
Jun 27, 2018 in Java by developer_1
• 3,220 points
8,239 views

10 answers to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

I might be barking up the wrong tree here, but your exception seems to indicate your MySQL server isn't available.

Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failureThe last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server. at...

What happens if you try (from the terminal)

mysql -u username -p

You will be prompted for the password associated with the username. After you give the correct password does the mysql client connect?

You may have to start MySQL from the Preferences if not. You can also set it to run at startup.

answered Jun 27, 2018 by developer_1
• 3,220 points
0 votes

I catch this exception when Java out of heap.
If I try to put in RAM many data items - first I catch "Communications link failure" and next "OutOfMemoryError".

I logged it and I decrease memory consumption (delete 1/2 data) and all ok.

Hope this helps.

answered Dec 10, 2018 by Shuvodip
0 votes

This com.mysql.jdbc.exceptions.jdbc4.CommunicationsException exception occurs if your database connection is idle for long time. 

answered Dec 10, 2018 by Shantanu
How to solve this? Coz we are using connection pooling C30 but still having this issue.
Hi @Subhrajit!

Can you tell me which database you are using and whether you are able to access it from the terminal?
0 votes

I will suggest to go with database pooling. As idle connection returns true on connection.isClosed(); but if we try to execute statement then it will fire this exception so 

answered Dec 10, 2018 by Sukesh
0 votes

Instead of using localhost:[Apache Port], use your MySQL port.

Below is the default MySQL Port for MAMP server.

String url = "jdbc:mysql://localhost:8889/db_name";

Connection conn = DriverManager.getConnection(url, dbUsername, dbPassword);

I was having the same problem and I did this and it worked fine.

answered Dec 10, 2018 by Bobin
0 votes
Connection con = DriverManager.getConnection(
 "jdbc:mysql://localhost:3306/DBname", "root", "root");

 Connection con = DriverManager.getConnection(
 "jdbc:mysql://192.100.0.000:3306/DBname", "root", "root");

Replacement of Localhost to your IP address. This can be the best solution and easy one too.

answered Dec 10, 2018 by bugseeker
0 votes
I got the same error because I was trying to run my program without starting mysql server.

After starting the mysql server, everything went right.
answered Dec 10, 2018 by Sneha
0 votes
I got this error because i didn't start my server before running this program.
answered Dec 10, 2018 by Naveen
0 votes

When you are using WAMP or XAMP server to install mysql database. Then you have to explicitly start mysql sever other wise it will show com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure while connecting with database

answered Dec 10, 2018 by Yamini
0 votes

It could be a simple jar problem. may be you are using a old mysql-connector-java-XXX-bin.jarwhich is not supported by your current mysql version. I used mysql-connector-java-5.1.18-bin.jaras i am using mysql 5.5 and this problem is resolved for me.

answered Dec 10, 2018 by User50789

Related Questions In Java

0 votes
1 answer

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: How to handle this exception?

You have a com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure java.net.ConnectException: ...READ MORE

answered Oct 8, 2018 in Java by parth295
• 4,590 points
146 views
0 votes
1 answer

How to connect Java program to the MySQL database?

You can connect your Java code with ...READ MORE

answered May 11, 2018 in Java by parth295
• 4,590 points
86 views
0 votes
2 answers

Can we catch multiple exceptions in the same catch clause in Java?

In Java 6 and before versions, we ...READ MORE

answered Aug 10, 2018 in Java by sharth
• 3,310 points
95 views
0 votes
1 answer

Find Oracle JDBC driver in Maven repository

Download the jar and place it in ...READ MORE

answered Sep 26, 2018 in Java by Daisy
• 8,020 points
397 views
0 votes
1 answer

How can I solve this: To prevent a memory leak, the JDBC Driver has been forcibly unregistered

What can you do? Ignore these warnings. Tomcat ...READ MORE

answered Jan 8 in Java by Daisy
• 8,020 points
1,070 views
0 votes
1 answer

What is ArrayIndexOutOfBoundsException ?

If You check System.out.print(names.length); you will get 3 ...READ MORE

answered May 11, 2018 in Java by sharth
• 3,310 points
37 views
0 votes
1 answer

How can we resolve ClassNotFoundException in Java?

Your classpath is broken. Depending on how you ...READ MORE

answered May 22, 2018 in Java by Akrati
• 3,150 points
25 views
0 votes
1 answer

How to resolve the java.net.ConnectException in Java?

If you are getting java.net.ConnectionException, it means ...READ MORE

answered Jun 4, 2018 in Java by Akrati
• 3,150 points
84 views
0 votes
1 answer

How can we resolve java.lang.OutOfMemoryError in Java?

Work with smaller batches of HashMap Objects ...READ MORE

answered Jun 5, 2018 in Java by sharth
• 3,310 points
38 views
0 votes
1 answer

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

Sqoop is not able to communicate with ...READ MORE

answered Jan 28 in Big Data Hadoop by Omkar
• 65,810 points
60 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.