jayesh
jayesh

Reputation: 2492

How to connect to a Cloud Foundry MySQL database connection in Java?

create mysql as service on Cloud Foundry and tunnel to mysql database this provides me connection string to mysql database i pass that information to my app. it works from my machine but when i deployed that app on Cloud Foundry server then it gives an error in connection

this is my connection code, tell me what needs to change to be deployed on Cloud Foundry

public class DB {
    private static Connection connection = null;
    public static Connection getConnection() {
        String url = "jdbc:mysql://127.0.0.1:10100/db8dad2d02e114ef6bc9d24e68367e33e";
        try {

            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection(url,"uC0ag3NRJCT8c","p1nyZ38zadwfa");
            System.out.println("Connect success fully");
            return connection;

        } catch (Exception e) {
            System.out.println("Error");
            System.out.println(e);
            e.printStackTrace();

        }
        return null;

    }

}

Upvotes: 0

Views: 8191

Answers (5)

Azimuts
Azimuts

Reputation: 1302

I have the same problem. You must notice that "10100" is a port fortwarding to the mysql remote service.

you could use this just locally.Deploying your program locally with your database connection pointing to the forwarding port (101100).

But this won't work when you push your war to the Cloud Foundry Instance-

One solution is to use Spring based cloud beans. In my case i don't wan't to use this approach so i'm trying another solution...

I don't know if with the credentials (user, password, tc) created for the remote connection you could stablish a connection once you pushed your war to Cloud Foundry changing the forwarding port and using the default mysql port (3360)

In my case i don't want to use Spring Cloud Beans because the production application won't be deployed into a cloud storage.

Upvotes: 1

anon
anon

Reputation:

You're using information from vmc tunnel to try to connect. This is not going to work on the Cloud. You need to do what jayesh shows, and read the connection credentials from the Cloud Foundry environment instead. Eric's answer is even more complete :-)

Upvotes: 1

ebottard
ebottard

Reputation: 1997

jayesh's answer is technically correct, but basically, the best way to deal with retrieving those information when inside a java app (assuming non-spring) is to use the cloudfoundry-runtime library: https://github.com/cloudfoundry/vcap-java/tree/master/cloudfoundry-runtime The README has examples of usage.

For completness, if using Spring, then things are even easier and chances are you don't even need to do anything special

Upvotes: 2

jayesh
jayesh

Reputation: 2492

    try {

        String vcap_services = System.getenv("VCAP_SERVICES");
        String hostname = "";
        String dbname = "";
        String user = "";
        String password = "";
        String port = "";
                //for cloud config
        if (vcap_services != null && vcap_services.length() > 0) {
            JsonRootNode root = new JdomParser().parse(vcap_services);

            JsonNode mysqlNode = root.getNode("mysql-5.1");
            JsonNode credentials = mysqlNode.getNode(0).getNode(
                    "credentials");

            dbname = credentials.getStringValue("name");
            hostname = credentials.getStringValue("hostname");
            user = credentials.getStringValue("user");
            password = credentials.getStringValue("password");
            port = credentials.getNumberValue("port");

            String dbUrl = "jdbc:mysql://" + hostname + ":" + port + "/"
                    + dbname;
            System.out.println(dbUrl);
            System.out.println(user + "password " + password);

            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection(dbUrl, user, password);
            return connection;
        } else {
//for local configuration
            Class.forName("com.mysql.jdbc.Driver");
            String url = jdbc:mysql://127.0.0.1:10100/db8dad2d02e114ef6bc9d24e68367e33e

            connection = DriverManager.getConnection(url, "user name",
                    "password");
            return connection;

        }

    } catch (Exception e) {

        e.printStackTrace();

    }

Upvotes: 1

Pradeep Simha
Pradeep Simha

Reputation: 18133

Problem is here:

jdbc:mysql://127.0.0.1:10100

In this you're connecting to 127.0.0.1, it is a localhost, try giving the actual IP of your cloud server. Then it should work fine.

Upvotes: 1

Related Questions