0

I've added a MySQL 5.5 cartridge on my RedHat openshift account within my openshift application. I've written the following Java program using JDBC, to connect to this MySQL database:

import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.DriverManager;

public class DBConnect {
    public static void main(String[] args) {
       String email = args[0];
       String password = args[1];
       try {
            Class.forName("com.mysql.jdbc.Driver");
            //Connection con = DriverManager.getConnection("jdbc:mysql://127.5.11.2:3306/funchat", "adminLvAZEDC", "G6Sm_Fl6C9A3");
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery("select * from users where Email = '" + email + "' and Password = '" + password + "'");
            if (rs.next()) {
               System.out.println("Login Successful");
            } else {
               System.out.println("Login failed");
            }
       }
       catch(Exception ex) {
          ex.printStackTrace();
       }

    }
}

I think, the parameters to the call DriverManager.getConnection are correct. I'm running this program remotely from a Windows 10 workstation. I'm getting following error when I run this program:

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(Unknown Source)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
        at java.lang.reflect.Constructor.newInstance(Unknown Source)
        at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
        at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:989)
        at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:341)
        at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2203)
        at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2236)
        at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2035)
        at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:790)
        at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
        at java.lang.reflect.Constructor.newInstance(Unknown Source)
        at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
        at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:400)
        at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:330)
        at java.sql.DriverManager.getConnection(Unknown Source)
        at java.sql.DriverManager.getConnection(Unknown Source)
        at DBConnect.main(DBConnect.java:12)
Caused by: java.net.ConnectException: Connection refused: connect
        at java.net.DualStackPlainSocketImpl.connect0(Native Method)
        at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
        at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
        at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
        at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
        at java.net.PlainSocketImpl.connect(Unknown Source)
        at java.net.SocksSocketImpl.connect(Unknown Source)
        at java.net.Socket.connect(Unknown Source)
        at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:211)
        at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:300)
        ... 15 more

Can someone please help me, to solve this problem?

mukul
  • 23
  • 1
  • 5
  • Note the IP address you are trying to connect: `127.5.11.2`. This is reserved non-routable IP address. Details in [Wikipedia](https://en.wikipedia.org/wiki/Reserved_IP_addresses#IPv4) - the fourth line `127.0.0.0/8`. You have to check the documentation of OpenShift how to connect remotely to the database instance. – zloster Apr 20 '17 at 06:48
  • Thanks for the pointer. This gives me some more knowledge about this problem. But I'm still looking for the solution. OpenShift site doesn't seem to offer guidance about this. – mukul Apr 20 '17 at 11:27
  • http://stackoverflow.com/questions/19749599/openshift-how-to-remote-access-mysql - there are several ways. I don't have idea which will serve you best. – zloster Apr 20 '17 at 15:16
  • Possible duplicate of [Openshift: How to remote access MySQL?](http://stackoverflow.com/questions/19749599/openshift-how-to-remote-access-mysql) – zloster Apr 20 '17 at 15:16

0 Answers0