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

in Java
Java Tutorials
2 Answers to this question

I catch this exception when the Java Out of heap.

If i try to put the same in the RAM many data items, first,  I catch "Communications link failure" and next "OutOfMemoryError".

I logged out of it and I decrease memory consumption  (Delete half of the data)  and all will be fine now.

This exception “Communications link failure: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException” may throw when we try to connect a java application to the database. It may have several reasons but we can’t tell the exact reasons and few reasons are given below:

This error “Communications link failure” may occurs if the given information related to server name(localhost) or database port(3306) in this Connection URL statement
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/DB","root","root");
is not correct.

This error may occur if the database server is down.
This error may occur if the database jar file is not compatible with the current database version that is used.
Don’t forget to start the database server before using.

To resolve this error, we perform few steps:

In the first step, we are using eclipse IDE and database MySQL Server 5.0. So, you can easily install eclipse and database server onto your machine.
Drag relevant mysql.jar file and drop it in the bin folder of Java JDK.
Before using database in eclipse , first we need to add libraries mysql.jar in eclipse IDE by following few steps:

Open Project in eclipse.
Right-click on Project-> Build Path -> Configure Build Path->Libraries->select classpath-> Add External JARs->select mysql.jar ->Apply and Close.


Finally, mysql.jar file is added successfully in eclipse and now we can work on JDBC with mysql efficiently and we have prepared full-fledged JDBC program in eclipse and everything is included in this program like

How to create table in mysql using JDBC
How to insert records into database table using JDBC
How to retrieve records from database table using JDBC
How to delete records from database tables using JDBC.

Now, we are going to share a sample program as per based on request and the program are given below:

For Example:

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.Statement;


public class JDBCProgram {

public static void main(String[] args) {

try {


// Load JDBC Driver



// Connection URL to the Database

Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/DB","root","root");


// Creating a Statement

Statement st = conn.createStatement();


/* Code for creating a table in Mysql using JDBC*/  


System.out.println("Create Statement....");  

      String query = "CREATE TABLE  FACULTY " +

                  "(id int not NULL, " +

                  " name VARCHAR(255), " + 

                  " address VARCHAR(20))";


      System.out.println("Faculty Table Created in database DB");



      /* Code for inserting records into a table FACULTY */

      /* in Mysql using JDBC */


      System.out.println("Insert Statement....");  

      query = "INSERT INTO FACULTY " +

                  "VALUES (1, 'Preeti', 'Delhi')";



      query = "INSERT INTO FACULTY " +

            "VALUES (2, 'Rajeev', 'Kanpur')";



      query = "INSERT INTO FACULTY " +

          "VALUES (3, 'Rahul', 'Roorkee')";



      query = "INSERT INTO FACULTY " +

    "VALUES (4, 'Anjali', 'Mumbai')";



      System.out.println("Records Inserted into Table FACULTY");




    /* Code for retrieving records from Table FACULTY*/  

                /* in Mysql using JDBC */


// Create ResultSet for Data Retrieval

ResultSet rs = st.executeQuery("select * from FACULTY");

System.out.println("Select Statement.....");

System.out.println("ID"+" "+"Name"+" "+"Address");

while( {

System.out.println(rs.getInt(1)+" "+rs.getString(2)

+" "+ rs.getString(3));


System.out.println("Records Retrieved From Table FACULTY");



/* Code for deleting records from Table FACULTY*/

/*in Mysql using JDBC*/  


System.out.println("Delete Statement.....");


"WHERE id = 4";


System.out.println("Record Deleted from Table FACULTY where id=4");




}catch(Exception ex) { 






Create Statement....
Faculty Table Created in database DB

Insert Statement....
Records Inserted into Table FACULTY

Select Statement.....
ID Name Address
1 Preeti Delhi
2 Rajeev Kanpur
3 Rahul Roorkee
4 Anjali Mumbai
Records Retrieved From Table FACULTY

Delete Statement.....
Record Deleted from Table FACULTY where id=4

If you want to unleash your potential in this competitive field, please visit the Java Certification Training page for more information, where you can find the       and    Java Interview Questions FAQ's and answers    as well.

For more updates on the latest courses stay tuned to HKR Trainings.

To Top