Ready to get started?

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

Download Now

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

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

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

Google Sheets Orders 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 MyOrdersDAO { //insert new data into Google Sheets @SqlUpdate("INSERT INTO Orders (ShipCity, OrderPrice) values (:shipCity, :orderPrice)") void insert(@Bind("shipCity") String shipCity, @Bind("orderPrice") String orderPrice); //request specific data from Google Sheets (String type is used for simplicity) @SqlQuery("SELECT OrderPrice FROM Orders WHERE ShipCity = :shipCity") String findOrderPriceByShipCity(@Bind("shipCity") String shipCity); /* * close with no args is used to close the connection */ void close(); }

Google Sheets への接続を開く

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

You can connect to a spreadsheet by providing authentication to Google and then setting the Spreadsheet connection property to the name or feed link of the spreadsheet. If you want to view a list of information about the spreadsheets in your Google Drive, execute a query to the Spreadsheets view after you authenticate.

ClientLogin (username/password authentication) has been officially deprecated since April 20, 2012 and is now no longer available. Instead, use the OAuth 2.0 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.

See the Getting Started chapter in the help documentation to connect to Google Sheets from different types of accounts: Google accounts, Google Apps accounts, and accounts using two-step verification.


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

java -jar cdata.jdbc.googlesheets.jar

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

A connection string for Google Sheets 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:googlesheets:Spreadsheet=MySheet;InitiateOAuth=GETANDREFRESH"); MyOrdersDAO dao =; //do stuff with the DAO dao.close();

Google Sheets Data について

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

//disply the result of our 'find' method String orderPrice = dao.findOrderPriceByShipCity("Madrid"); System.out.println(orderPrice);

Google Sheets Data の書き方

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

//add a new entry to the Orders entity dao.insert(newShipCity, newOrderPrice);

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