Over a million developers have joined DZone.

CouchDB: Adding Document Using Java Couchdb4j

· Database Zone

To stay on top of the changing nature of the data connectivity world and to help enterprises navigate these changes, download this whitepaper from Progress Data Direct that explores the results of the 2016 Data Connectivity Outlook survey.


  • Couchdb4j is a library for Couch Database for manipulating document in database.
  • The jar file :-
           http://code.google.com/p/couchdb4j/downloads/list
  • In this Demo ,"A new Student document is created with properties nad added to the student database".
  • Project structure:-

  • The  Java code CouchDBTest.java is ,
package com.sandeep.couchdb.util;

import java.util.HashMap;
import java.util.Map;
import com.fourspaces.couchdb.Database;
import com.fourspaces.couchdb.Document;
import com.fourspaces.couchdb.Session;

public class CouchDBTest {

 /*These are the keys of student document in couch db*/
 public static final String STUDENT_KEY_NAME ="name";
 
 public static final String STUDENT_KEY_MARKS ="marks";
 
 public static final String STUDENT_KEY_ROLL="roll";
 
 
 public static void main(String[] args){
  
  /*Creating a session with couch db running in 5984 port*/
  Session studentDbSession = new Session("localhost",5984);
  
  /*Selecting the 'student' database from list of couch database*/
  Database studentCouchDb = studentDbSession.getDatabase("student");
  
  /*Creating a new Document*/
  Document newdoc = new Document();
  
  /*Map for list of properties for the new document*/
  Map<String , String> properties = new HashMap<String,String>();
  
  properties.put(STUDENT_KEY_NAME, "saan");
  
  properties.put(STUDENT_KEY_MARKS, "67");
  
  properties.put(STUDENT_KEY_ROLL, "12");
  
  
  /*Adding all the properties to the new document*/
  newdoc.putAll(properties);
  
  /*Saving the new document in the 'student' database */
  studentCouchDb.saveDocument(newdoc);  
  
 }
}
  • We can open the Futon and verify that the document is added to "student" Database.The screenshot,

Turn Data Into a Powerful Asset, Not an Obstacle with Democratize Your Data, a Progress Data Direct whitepaper that explains how to provide data access for your users anywhere, anytime and from any source.

Topics:

Published at DZone with permission of Sandeep Patel, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}