Ready to get started?

Learn more about the CData JDBC Driver for MongoDB or download a free trial:

Download Now

Create a Data Access Object for MongoDB Data using JDBI

A brief overview of creating a SQL Object API for MongoDB 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 MongoDB integrates connectivity to live MongoDB data in Java applications. By pairing these technologies, you gain simple, programmatic access to MongoDB data. This article walks through building a basic Data Access Object (DAO) and the accompanying code to read and write MongoDB data.

Create a DAO for the MongoDB restaurants 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 MyrestaurantsDAO { //insert new data into MongoDB @SqlUpdate("INSERT INTO restaurants (Name, cuisine) values (:name, :cuisine)") void insert(@Bind("name") String name, @Bind("cuisine") String cuisine); //request specific data from MongoDB (String type is used for simplicity) @SqlQuery("SELECT cuisine FROM restaurants WHERE Name = :name") String findcuisineByName(@Bind("name") String name); /* * close with no args is used to close the connection */ void close(); }

Open a Connection to MongoDB

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

Set the Server, Database, User, and Password connection properties to connect to MongoDB. To access MongoDB collections as tables you can use automatic schema discovery or write your own schema definitions. Schemas are defined in .rsd files, which have a simple format. You can also execute free-form queries that are not tied to the schema.

Built-in Connection String Designer

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

java -jar cdata.jdbc.mongodb.jar

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

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

jdbc:mongodb:Server=MyServer;Port=27017;Database=test;User=test;Password=Password;

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:mongodb:Server=MyServer;Port=27017;Database=test;User=test;Password=Password;"); MyrestaurantsDAO dao = dbi.open(MyrestaurantsDAO.class); //do stuff with the DAO dao.close();

Read MongoDB Data

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

//disply the result of our 'find' method String cuisine = dao.findcuisineByName("Morris Park Bake Shop"); System.out.println(cuisine);

Write MongoDB Data

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

//add a new entry to the restaurants entity dao.insert(newName, newcuisine);

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