Advertisement
Guest User

Untitled

a guest
Sep 14th, 2017
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.76 KB | None | 0 0
  1. package com.iba.edu;
  2.  
  3. import com.cloudant.client.api.ClientBuilder;
  4. import com.cloudant.client.api.CloudantClient;
  5. import com.cloudant.client.api.Database;
  6.  
  7. import java.io.IOException;
  8. import java.io.InputStream;
  9. import java.util.List;
  10. import java.util.Properties;
  11.  
  12. public class Cloudant_test {
  13.  
  14.     public static  void main(String[] args) {
  15.         Properties prop = new Properties();
  16.         InputStream input = null;
  17.  
  18.         try {
  19.  
  20.             String filename = "cloudant.properties";
  21.             input = Cloudant_test.class.getClassLoader().getResourceAsStream(filename);
  22.             if (input == null) {
  23.                 System.out.println("Sorry, unable to find " + filename);
  24.                 return;
  25.             }
  26.  
  27.             prop.load(input);
  28.  
  29.         } catch (IOException ex) {
  30.             ex.printStackTrace();
  31.         } finally {
  32.             if (input != null) {
  33.                 try {
  34.                     input.close();
  35.                 } catch (IOException e) {
  36.                     e.printStackTrace();
  37.                 }
  38.             }
  39.         }
  40.  
  41.         CloudantClient client = ClientBuilder.account(prop.getProperty("account"))
  42.                 .username(prop.getProperty("user"))
  43.                 .password(prop.getProperty("password"))
  44.                 .build();
  45.  
  46.         System.out.println("-----------------------------------------");
  47.         System.out.println("Connected to Cloudant");
  48.         System.out.println("Server Version: " + client.serverVersion());
  49.         System.out.println("-----------------------------------------");
  50.  
  51.         List<String> databases = client.getAllDbs();
  52.         System.out.println("-----------------------------------------");
  53.         System.out.println("All my databases : ");
  54.         for ( String db : databases ) {
  55.             System.out.println(db);
  56.         }
  57.         System.out.println("-----------------------------------------");
  58.  
  59. //        client.deleteDB("db_to_delete");
  60. //        client.createDB("example_db");
  61.  
  62.         Database db = client.database("employee_directory", false);
  63. //        db.save(new ExampleDocument(true));
  64. //        System.out.println("You have inserted the document");
  65. //        System.out.println("-----------------------------------------");
  66.  
  67.         ExampleDocument doc = db.find(ExampleDocument.class,"example_id");
  68.         System.out.println(doc);
  69.  
  70.  
  71.     }
  72. }
  73.  
  74.  
  75.  
  76. -------
  77.  
  78. package com.iba.edu;
  79.  
  80. public class ExampleDocument {
  81.  
  82.     private String _id = "example_id";
  83.     private String _rev = null;
  84.     private boolean isExample;
  85.  
  86.     public ExampleDocument(boolean isExample) {
  87.         this.isExample = isExample;
  88.     }
  89.  
  90.     public String toString() {
  91.         return "{ id: " + _id + ",\nrev: " + _rev + ",\nisExample: " + isExample + "\n}";
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement