From Chocolate Mousedeer, 9 Years ago, written in Plain Text.
Embed
  1. package com.pearson.cbl;
  2.  
  3. import com.couchbase.lite.Manager;
  4.  
  5. public class Main {
  6.  
  7.     public static void main(String[] args) {
  8.  
  9.  
  10. // Enable logging
  11.         Logger log = Logger.getLogger("app");
  12.         log.setLevel(Level.ALL);
  13.         JavaContext context = new JavaContext();
  14. // Create a manager
  15.         Manager manager = null;
  16.         try {
  17.             manager = new Manager(context, Manager.DEFAULT_OPTIONS);
  18.         } catch (IOException e) {
  19.             e.printStackTrace();
  20.         }
  21. // Create or open the database named app
  22.         Database database = null;
  23.         try {
  24.             database = manager.getDatabase("app");
  25.         } catch (CouchbaseLiteException e) {
  26.             e.printStackTrace();
  27.         }
  28. // The properties that will be saved on the document
  29.         Map<String, Object> properties = new HashMap<String, Object>();
  30.         properties.put("title", "Couchbase Mobile");
  31.         properties.put("sdk", "Java");
  32. // Create a new document
  33.         Document document = database.createDocument();
  34. // Save the document to the database
  35.         try {
  36.             document.putProperties(properties);
  37.         } catch (CouchbaseLiteException e) {
  38.             e.printStackTrace();
  39.         }
  40. // Log the document ID (generated by the database)
  41. // and properties
  42.         log.info(String.format("Document ID :: %s", document.getId()));
  43.         log.info(String.format("Learning %s with %s", (String) document.getProperty("title"), (String) document.getProperty("sdk")));
  44.  
  45.     }
  46. }
  47.