Search code examples
cloudantibm-cloud

how to connect a Java app ( java code ) to cloudant?


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 ?


Solution

  • 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!