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

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

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

GDrive 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 MyGDriveDAO { //insert new data into @SqlUpdate("INSERT INTO GDrive (Formats, Size) values (:formats, :size)") void insert(@Bind("formats") String formats, @Bind("size") String size); //request specific data from (String type is used for simplicity) @SqlQuery("SELECT Size FROM GDrive WHERE Formats = :formats") String findSizeByFormats(@Bind("formats") String formats); /* * close with no args is used to close the connection */ void close(); }


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

Google uses the OAuth authentication standard. To access Google APIs on behalf on individual users, you can use the embedded credentials or you can register your own OAuth app.

OAuth also enables you to use a service account to connect on behalf of users in a Google Apps domain. To authenticate with a service account, you will need to register an application to obtain the OAuth JWT values.

In addition to the OAuth values, to access Google data you will need the Email of the Google account. See the "Getting Started" chapter in the help documentation for a guide to using OAuth.


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

java -jar cdata.jdbc.googleapps.jar

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

A connection string for 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:googleapps:OAuthClientId=MyOAuthClientId;OAuthClientSecret=MyOAuthClientSecret;CallbackURL=http://localhost;Email=myemail@gmail.com;InitiateOAuth=GETANDREFRESH"); MyGDriveDAO dao = dbi.open(MyGDriveDAO.class); //do stuff with the DAO dao.close();

Data について

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

//disply the result of our 'find' method String size = dao.findSizeByFormats("DOC"); System.out.println(size);

Data の書き方

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

//add a new entry to the GDrive entity dao.insert(newFormats, newSize);

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