how to connect a Java app ( java code ) to cloudan

2019-07-24 08:50发布

问题:

I've java code in eclipse and I've done all the set up required between eclipse and IBM bluemix cloudant service I am not sure how to update my code to enable cloudant in eclipse can someone please help ?

回答1:

you need to add a piece of code in CloudantClient.java file under source directory of your project. Please add these lines in CloudantClient class:

String VCAP_SERVICES = System.getenv("VCAP_SERVICES");
                JSONObject vcap;
                vcap = (JSONObject) JSONObject.parse(VCAP_SERVICES);
                cloudant = (JSONArray) vcap.get("cloudantNoSQULDB");
                cloudantInstance = (JSONObject) cloudant.get(0);
                cloudantCredentials = (JSONObject) cloudantInstance.get("credentials");

you can also put this piece of code in a try catch loop as well.

try {
                String VCAP_SERVICES = System.getenv("VCAP_SERVICES");
                JSONObject vcap;
                vcap = (JSONObject) JSONObject.parse(VCAP_SERVICES);
                cloudant = (JSONArray) vcap.get("cloudantNoSQULDB");
                cloudantInstance = (JSONObject) cloudant.get(0);
                cloudantCredentials = (JSONObject) cloudantInstance.get("credentials");
                } 
            catch (IOException e) {
                e.printStackTrace();
            }

I hope it works!



回答2:

You may use the Bluemix config parser library to automatically parse the VCAP_SERVICES env variable (https://github.com/icha024/bluemix-config-parser)

It simplifies the messy code into...

String username = BluemixConfigStore.getConfig().getCloudantNoSQLDB()
                    .getCredentials().getUsername();
String password = BluemixConfigStore.getConfig().getCloudantNoSQLDB()
                    .getCredentials().getPassword();    

Then you can create a Cloudant client from it as usual:

CloudantClient cloudantClient = ClientBuilder.account(username)
                                  .username(username)
                                  .password(password)
                                  .build();


回答3:

You need to use VCAP_SERVICES env variable used in bluemix like below:

private JSONArray cloudant;
private JSONObject cloudantInstance;
private JSONObject cloudantCredentials;
public CloudantClient()
{
    this.httpClient = null;

    try {
        String VCAP_SERVICES = System.getenv("VCAP_SERVICES");
        JSONObject vcap;
        vcap = (JSONObject) JSONObject.parse(VCAP_SERVICES);
        cloudant = (JSONArray) vcap.get("cloudantNoSQLDB");
        cloudantInstance = (JSONObject) cloudant.get(0);
        cloudantCredentials = (JSONObject) cloudantInstance.get("credentials");
    } catch (IOException e) {
        e.printStackTrace();
    }
    this.port = Config.CLOUDANT_PORT;
    this.host = (String) cloudantCredentials.get("host");
    this.username = (String) cloudantCredentials.get("username");
    this.password = (String) cloudantCredentials.get("password");
    this.name = Config.CLOUDANT_NAME;
    this.dbc = this.createDBConnector(); 
}