Ready to get started?

Download a free trial of the CouchDB Driver to get started:

 Download Now

Learn more:

CouchDB Icon CouchDB JDBC Driver

Rapidly create and deploy powerful Java applications that integrate with CouchDB.

Create a Data Access Object for CouchDB Data using JDBI



A brief overview of creating a SQL Object API for CouchDB data in JDBI.

JDBI is a SQL convenience library for Java that exposes two different style APIs, a fluent style and a SQL object style. The CData JDBC Driver for CouchDB integrates connectivity to live CouchDB data in Java applications. By pairing these technologies, you gain simple, programmatic access to CouchDB data. This article walks through building a basic Data Access Object (DAO) and the accompanying code to read and write CouchDB data.

Create a DAO for the CouchDB Movies Entity

The interface below declares the desired behavior for the SQL object to create a single method for each SQL statement to be implemented.

public interface MyMoviesDAO { //insert new data into CouchDB @SqlUpdate("INSERT INTO Movies (MovieRating, MovieRating) values (:movieRating, :movieRating)") void insert(@Bind("movieRating") String movieRating, @Bind("movieRating") String movieRating); //request specific data from CouchDB (String type is used for simplicity) @SqlQuery("SELECT MovieRating FROM Movies WHERE MovieRating = :movieRating") String findMovieRatingByMovieRating(@Bind("movieRating") String movieRating); /* * close with no args is used to close the connection */ void close(); }

Open a Connection to CouchDB

Collect the necessary connection properties and construct the appropriate JDBC URL for connecting to CouchDB.

Set the following to connect:

  • Url: The Url of your instance. For example: http://localhost:5984
  • User The Apache CouchDB user account used to authenticate.
  • Password The Apache CouchDB password associated with the authenticating user.

Built-in Connection String Designer

For assistance in constructing the JDBC URL, use the connection string designer built into the CouchDB JDBC Driver. Either double-click the JAR file or execute the jar file from the command-line.

java -jar cdata.jdbc.apachecouchdb.jar

Fill in the connection properties and copy the connection string to the clipboard.

A connection string for CouchDB will typically look like the following:

jdbc:apachecouchdb:Url=http://localhost:5984;User=abc123;Password=abcdef;

Use the configured JDBC URL to obtain an instance of the DAO interface. The particular method shown below will open a handle bound to the instance, so the instance needs to be closed explicitly to release the handle and the bound JDBC connection.

DBI dbi = new DBI("jdbc:apachecouchdb:Url=http://localhost:5984;User=abc123;Password=abcdef;"); MyMoviesDAO dao = dbi.open(MyMoviesDAO.class); //do stuff with the DAO dao.close();

Read CouchDB Data

With the connection open to CouchDB, simply call the previously defined method to retrieve data from the Movies entity in CouchDB.

//disply the result of our 'find' method String movieRating = dao.findMovieRatingByMovieRating("R"); System.out.println(movieRating);

Write CouchDB Data

It is also simple to write data to CouchDB, using the previously defined method.

//add a new entry to the Movies entity dao.insert(newMovieRating, newMovieRating);

Since the JDBI library is able to work with JDBC connections, you can easily produce a SQL Object API for CouchDB by integrating with the CData JDBC Driver for CouchDB. Download a free trial and work with live CouchDB data in custom Java applications today.