• Register
0 votes
218 views

Problem :

I want my database to talk to my Java programs.

I am getting following error message.

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:48)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:36)
at java.lang.reflect.Constructor.newInstance(Constructor.java:612)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:518)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:2233)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:3351)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:999)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:56)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:40)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:30)
at java.lang.reflect.Constructor.newInstance(Constructor.java:600)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:512)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:410)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:300)
at java.sql.DriverManager.getConnection(DriverManager.java:600)
at java.sql.DriverManager.getConnection(DriverManager.java:310)
at SqlTest.main(SqlTest.java:33)
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.

Can somebody give me the quick and easy fix for my issue?

Contents of my test file as below:

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 JdbcSqlTest {

public static void main(String [] args) throws Exception {

 // Class.forName( "com.mysql.jdbc.Driver" ); // do this in init

        // // edit the jdbc url

        Connection connect = DriverManager.getConnection(

            "jdbc:mysql://localhost:3310/projects?user=user1&password=123");

        // Statement stat = connect.createStatement();

        // ResultSet result = stat.executeQuery( "select * from table" );

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

    }

}

6 5 3
7,540 points

2 Answers

0 votes

Solution :

This is best and easiest solution for you,

Connection connect = DriverManager.getConnection( 

"jdbc:mysql://localhost:3310/DBname", "root", "root"); 

Connection connect = DriverManager.getConnection( 

"jdbc:mysql://192.100.0.000:3310/DBname", "root", "root");

I just Replaced the Localhost with your IP address.

9 7 4
38,600 points
0 votes

If you get an SQLException: Connection refused or connection timed out or a MySQL specific CommunicationException:

Communication links failure then it means that the DB is not reachable at all.

Reasons:

This error can have one or more reasons that are described as;

  • IP address or hostname in JDBC URL is wrong.
  • The hostname in JDBC is not recognized by the local DNS server.
  • Port number is missing or wrong in JDBC URL.
  • The database server is down.
  • The database server does not accept the TCP/IP connections.
  • The database server has run out of connections.
  • Something is between Java and Database has blocked the connection like a firewall or proxy.

Solution:

To solve one or the other error, just follow the following procedure;

  • Verify and test them with ping.
  • Refresh DNS or use an IP address in JDBC URL instead.
  • Verify it based on my.cnf of MYSQL database.
  • Start the database.
  • Verify if MySQL is started without using the –skip-networking option.
  • Restart the database and fix your code accordingly that it closes the connection finally.
  • Disable the firewall or configure the firewall to forward the port.

Replacement of localhost:

This error can also be solved by the replacement of the localhost to the actual database server IP address.

Replace from;

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

To;

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

 

 

11 5 1
3,890 points

Related questions

0 votes
1 answer 111 views
111 views
Problem : Facing following jdbc issue caused by: com.mysql.jdbc.exceptions.jdbc4.communicationsexception: communications link failure
asked Nov 13, 2019 peterlaw 6.9k points
0 votes
1 answer 10 views
10 views
Problem: I want to check if the very specific value already exists in the database. I am currently accessing database from the java standalone app using the JDBC my queries for inserting records into the db are working so my setup and the connection are perfectly fine. ... near 'd-f05708071f8f' at line 1 But if I attempt to execute above command in the MySQL command line then it works properly.
asked Aug 3 Raphael Pacheco 4.9k points
0 votes
1 answer 6 views
6 views
I need to know how to return multiple values from a function in Java.
asked Oct 3 Daniel Anderson 4k points
0 votes
1 answer 11 views
11 views
I need to know how to update radio button value in database using PHP.
asked Sep 29 Daniel Anderson 4k points
0 votes
1 answer 9 views
9 views
How can i can pass data between view and controller. any one here guide me ?
asked Sep 26 Daniel Anderson 4k points