Channels ▼
RSS

Web Development

Apache CouchDB and Java


Creating the Database and Other Database Operations

Once the Session is established you can start creating a database. The first step in this case is to create a database where you will store your data in the documents. A database must be created with lowercase characters (a-z), or digits (0-9), or any of the _$()+-/ characters and it must end with a slash in the URL. The name of the database has to start with characters. Also, a / character in a database name must be escaped when used in a URL. You can now create a database named "employee":


String dbname = "employee";
dbSession.createDatabase(dbname);

Here dbSession is the Session object which we created earlier and createDatabase is the method used to create database. The parameter for the createDatabase method is the name of the database (in String).

You can also retrieve the list of all the databases that are present in the CouchDB server. The following code shows how:


List <String> listofdb = dbSession.getDatabaseNames();

Here listofdb is the String list which will hold the result (that are the names of databases present) obtained by calling the getDatabaseNames method.


String dbname = "employee";
dbSession.deleteDatabase(dbname);

Here dbSession is the Session object which created earlier. Parameters for the deleteDatabase method will be the name of the database (in String).

Inserting Documents (Data) Into the Database and Other Document Operations

Once you have created a database named "employee", you need to enter data to the database. With CouchDB, data is in the form of documents, so your next step will be to create documents for "employee" database.

Documents stored in a CouchDB have a document id -- DocID. DocIDs are case-sensitive string identifiers that uniquely identify a document. Documents can be created in two ways:

  • By providing DocID
  • By not providing DocID, in which case CouchDB will generate a random DocID for the document.

Now consider that you are having data of employee's in an organization:

Table 1

Here EmpNO, Name, Group, Designation, Language are the field names. You can create a document for each of the employee (i.e. for each row in Table 1) providing information for each field as shown in Table 1. You will create all the documents inside the database "employee". In this case, I provide the DocID and it will be same as EmpNo field for each employee.

The following code illustrations the process of creating a document for the first employee (for first row data) only, similarly documents can be created for other employees also:


String dbname = "employee";
Database db = dbSession.getDatabase(dbname);
		
Document doc = new Document();
		
doc.setId("1");
doc.put("EmpNO", "1");
doc.put("Name", "Mike");
doc.put("Group", "J2EECOE");
doc.put("Designation", "Manager");
doc.put("Language", "Java");
		
db.saveDocument(doc);

In this code, the dbSession.getDatabase(dbname) method will get the database object for the "employee" database. The doc.setId("1") method is used to set the DocID for the document, in this case, it create a document with DocID as 1. The doc.put("EmpNO","1") method is used to enter data to the document (i.e. enter field and its values), first parameter of the method is Field Name and second parameter is Value, so EmpNO is the field name and 1 is the value for the field. Once all the fields and its values are entered in the document, you save the document using the db.saveDocument(doc) method.

If you have data in text file, then you can write a code to read file and put each row data as a document in CouchDB database. If you do not want to give DocID for each document and want CouchDB to assign DocID, then just comment out the line doc.setId("1") in the above example.

You can also retrieve the total number of documents in a particular database. The following code shows the retrieval of total number of documents in the "employee" database:


String dbname = "employee";
Database db = dbSession.getDatabase(dbname);
		
int count = db.getDocumentCount();
System.out.println("Total Documents: " + count);

The result:


Total Documents: 9

Here the db.getDocumentCount() method is used to retrieve the number of documents in database.

If necessary, you can also delete a particular document from the database. The following code shows how to delete a document whose id is "1" from the "employee" database:


String dbname = "employee";
Database db = dbSession.getDatabase(dbname);
		
Document d = db.getDocument("1");
db.deleteDocument(d);

Here we will first retrieve the document which we want to delete by using the db.getDocument("1") method, which takes DocID as input parameter. Then we will delete the retrieved document by using the db.deleteDocument(d) method.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 
Dr. Dobb's TV