• Register
0 votes
374 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

Please log in or register to answer this question.

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 2
3,890 points

Related questions

0 votes
1 answer 126 views
126 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 57 views
57 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, 2020 Raphael Pacheco 4.9k points
0 votes
1 answer 13 views
13 views
Problem : I am a completely novice in the hibernate world and facing this error: Generationtarget encountered exception accepting command : error executing ddl via jdbc statement .
asked Mar 13 Wafa Abu Yousef 6.1k points
1 vote
1 answer 38 views
38 views
Problem : HELP! I need help understanding this error: Error creating bean with name 'employeeRepositoryDAOImple': Unsatisfied dependency expressed through method 'setSessionFactory' parameter 0; nested exception is org.springframework.beans.factory. ... failed; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to open JDBC Connection for DDL execution
asked Mar 12 Shovo210 14k points
0 votes
1 answer 50 views
50 views
Problem: why I cant fix this : Unable to create requested service [org.hibernate.engine.jdbc.env.spi.jdbcenvironment] .
asked Mar 6 Wafa Abu Yousef 6.1k points
0 votes
1 answer 29 views
29 views
I am facing problem creating a jbdc driver of class. I want to connect it to url null.
asked Sep 22, 2020 Daniel Anderson 4k points
0 votes
1 answer 2.3K views
2.3K views
Problem : I am a completely novice in the hibernate world and facing below error, WARN: GenerationTarget encountered exception accepting command : Error executing DDL via JDBC Statement org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL via JDBC Statement The ... hibernate 4 version all my code runs very well. I looked for many solutions but they are not working for me.
asked Feb 4, 2020 jwilliam 3.9k points
0 votes
1 answer 4.2K views
4.2K views
Problem : I want to configure the hibernate orm mapping tool to my java class and want to use PostgreSQL as my database also I have configured the password as "password". But when I tried to run the application, I am facing following error ... in thread "main" org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
asked Dec 4, 2019 alecxe 7.5k points
0 votes
1 answer 106 views
106 views
Problem : My Java Servlet program showing the bellow Exception java.lang.classnotfoundexception oracle.jdbc.driver.oracledriver
asked Nov 5, 2019 peterlaw 6.9k points
0 votes
0 answers 3 views
3 views
Problem: Hi I am facing this iisue can some one help me? column count doesn't match value count at row 1
asked Mar 20 PkGuy 13.1k points