None of the solutions I have found online works. I keep hitting
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
I am trying to connect to a MySQL db from another server.
I can assure you that the url, user and password information is correct as it works when I try to connect the db using PgAdmin
software. However, it fails when I try executing using Java code (via NetBean 8.1).
Ok, so the following is my code:
import oracle.jdbc.pool.OracleDataSource;
import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Client9{
public static void main(String[] args) {
System.out.println("MySQL Connect Example.");
Connection conn = null;
String url = "jdbc:mysql://MySQLUrl:3306/";
String dbName = "MySQL_dbName";
String driver = "com.mysql.jdbc.Driver";
String userName = "user";
String password = "password";
try {
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url+dbName,userName,password);
System.out.println("Connected to the database");
conn.close();
System.out.println("Disconnected from database");
} catch (Exception e) {
e.printStackTrace();
}
}
}
Error message is shown below:
run:
MySQL Connect Example.
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:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1117)
at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:673)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1084)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2465)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2498)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2283)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:822)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:404)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:317)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at netiqcustomprov.Client9.main(Client9.java:35)
Caused by: java.io.EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.
at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:3052)
at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:597)
... 16 more
BUILD SUCCESSFUL (total time: 1 minute 0 seconds)
Solutions I have tried so far:
I have imported ojdb14.jar
and mysql-connector-java-5.1.23-bin.jar
into the library.
I have tried all 3 different methods getConnection()
getConnection(String url)
(where my string url = "jdbc:mysql://MySQLUrl:3306/MySQL_dbName?user=user&password=password")getConnection(String url, Properties info)
getConnection(String url, String user, String password)
I have also tried to register the driver using the following command:
Driver myDriver = new oracle.jdbc.driver.OracleDriver();
DriverManager.registerDriver( myDriver );
One of the solution I found is to comment the "skip-networking" rule in my.cnf file. However, I could not find such file in the server. (The server operates based on Linux OS)
Greatly appreciate the assistance in solving this issue. Thanks in advance.
UPDATE: See my answer. Once again I apologise for my silly mistake.