1

I am having issues connecting to mySQL server using ConnectJ driver for JDBC. I wrote a simple program just to establish connectivity as below:

private static final String DB_DRIVER = "com.mysql.jdbc.Driver";
private static final String DB_CONNECTION = "jdbc:mysql://[host]:1521/OBIEE11G";    
private static final String DB_USER = "OBADW";
private static final String DB_PASSWORD = "OBADW";

public static Connection getConnection() {

    Connection dbConnection = null;
    try {
        // DriverManager.registerDriver(new com.mysql.jdbc.Driver());
        Class.forName(DB_DRIVER);
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
        System.out.println(e.getMessage());
    }
    try {
        // DriverManager.registerDriver(new com.mysql.jdbc.Driver());
        System.out.println("before get connection ***********");
        System.out.println("Java version: " + System.getProperty("java.version"));
        Driver driver = (Driver) Class.forName("com.mysql.jdbc.Driver").newInstance();
        System.out.println("JDBC driver: " + driver.getMajorVersion() + "." + driver.getMinorVersion());
        dbConnection = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD);
        System.out.println("Connection: " + dbConnection.getClass().getName());
        System.out.println("after get connection ***********");
        return dbConnection;
    } catch (SQLException | InstantiationException | IllegalAccessException | ClassNotFoundException e) {
        e.printStackTrace();
        System.out.println(e.getMessage());
    }
    return dbConnection;
}

public static void main(String[] args) {
    Connection dbConnection = getConnection();

    if (dbConnection != null) {
        System.out.println("Connected");
    } else {
        System.out.println("Failed");
    }

I am getting the following error:

before get connection ***********
Java version: 1.7.0
JDBC driver: 5.1
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:85)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:57)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:541)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:983)
    at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:628)
    at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1014)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2254)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2285)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2084)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:795)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:44)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:85)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:57)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:541)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:400)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:327)
    at java.sql.DriverManager.getConnection(DriverManager.java:582)
    at java.sql.DriverManager.getConnection(DriverManager.java:226)
    at JDBCConnection.getConnection(JDBCConnection.java:32)
    at JDBCConnection.main(JDBCConnection.java:45)
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:2949)
    at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:560)
    ... 17 more
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. Failed

I am using connectJ : mysql-connector-java-5.1.37-bin.jar Java 1.7, JDBC driver 5.1

I am able to ping the host, and connect it using mysql developer. Please suggest.

sagabh
  • 11
  • 3
  • yes, it failing at dbConnection = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD); line, I also pinged the server, and its seems to be up and running – sagabh Jan 22 '16 at 03:20
  • Possible duplicate of [com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure](http://stackoverflow.com/questions/2983248/com-mysql-jdbc-exceptions-jdbc4-communicationsexception-communications-link-fai) – Tim Biegeleisen Jan 22 '16 at 03:21
  • yes, i went through this post, however, i am able to connect to the host using my local mysql developer. It doesnt look like the solution described in the post – sagabh Jan 22 '16 at 03:25
  • Your `DB_CONNECTION` string appears to be specifying port `1521`, but the default port for MySQL is `3306`. Are you certain this is correct? – Tim Biegeleisen Jan 22 '16 at 03:28
  • Yep, i checked the properties of the db connectivity, its 1521. – sagabh Jan 22 '16 at 03:29
  • This sounds suspicious to me. Did you manually reassign the port to `1521`? In which db file are you looking? – Tim Biegeleisen Jan 22 '16 at 03:33
  • Could you solve this problem i also in the same situation is there a any solution for it [1]: http://i.stack.imgur.com/3Jlw4.png [2]: http://i.stack.imgur.com/GUCx3.png – Thilina Dharmasena Jul 28 '16 at 10:45

2 Answers2

1

From your log its clear about "Communications link failure"

  • ensure your mysql server running
  • ensure you have the right hostname/ip/port
  • ensure your network is working
1

Sounds stupid to ask but have you configured your MySQL Connector.jar file correctly in your project properties?