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

2019-07-24 08:44发布

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 ?

3条回答
乱世女痞
2楼-- · 2019-07-24 09:00

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(); 
}
查看更多
看我几分像从前
3楼-- · 2019-07-24 09:10

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();
查看更多
萌系小妹纸
4楼-- · 2019-07-24 09:14

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!

查看更多
登录 后发表回答