Ready to get started?

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

Download Now

JDBI からMongoDB Data のデータアクセスオブジェクトを作成

JDBI でMongoDB data 用のSQL オブジェクトAPIを作成する方法を概説します。

JDBI は、Fluent スタイルとSQL オブジェクトスタイルという2つの異なるスタイルAPI を公開する、Java 用のSQL コンビニエンスライブラリです。CData JDBC Driver for MongoDB は、Java アプリケーションとリアルタイムMongoDB data のデータ連携を実現します。これらの技術を組み合わせることによって、MongoDB data へのシンプルなコードアクセスが可能になります。ここでは、基本的なDAO(Data Access Object )とそれに付随するMongoDB data の読み書きのためのコードの作成について説明します。

MongoDB restaurants Entity のDAO を作成

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(); }

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.


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:


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 =; //do stuff with the DAO dao.close();

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);

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.