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

Java

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).......

 

 


If you want to unleash your potential in this competitive field, please visit the Java course page for more information, where you can find the Java tutorials and Java frequently asked interview questions and answers as well.

2
Answers

Replies

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

Class.forName("com.mysql.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))";


      st.executeUpdate(query);

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

      System.out.println();

      

      /* 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')";

      st.executeUpdate(query);

      

      query = "INSERT INTO FACULTY " +

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

      st.executeUpdate(query);

     

      query = "INSERT INTO FACULTY " +

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

      st.executeUpdate(query);

    

      query = "INSERT INTO FACULTY " +

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

      st.executeUpdate(query);

  

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

      System.out.println();         

 

     

    /* 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(rs.next()) {

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

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

}

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

System.out.println();

 

/* Code for deleting records from Table FACULTY*/

/*in Mysql using JDBC*/  

 

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

query = "DELETE FROM FACULTY " +

"WHERE id = 4";

st.executeUpdate(query);

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

System.out.println();

 

conn.close();

}catch(Exception ex) { 

 

System.out.println(ex);

}

}


}

Output:
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

 
 

This topic has been locked/unapproved. No replies allowed

Login to participate in this discussion.

Leave a reply

Before proceeding, please check your email for a verification link. If you did not receive the email, click here to request another.
WhatsApp
To Top