Ready to get started?

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

Download Now

Create a Data Access Object for Google Directory Data using JDBI

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

Create a DAO for the Google Directory MyTable 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 MyMyTableDAO { //insert new data into Google Directory @SqlUpdate("INSERT INTO MyTable (Status, Description) values (:status, :description)") void insert(@Bind("status") String status, @Bind("description") String description); //request specific data from Google Directory (String type is used for simplicity) @SqlQuery("SELECT Description FROM MyTable WHERE Status = :status") String findDescriptionByStatus(@Bind("status") String status); /* * close with no args is used to close the connection */ void close(); }

Open a Connection to Google Directory

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

Google uses the OAuth authentication standard. You can authorize the data provider to access Google Spreadsheets as an individual user or with a Google Apps Domain service account. See the Getting Started section of the data provider help documentation for an authentication guide.

Built-in Connection String Designer

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

java -jar cdata.jdbc.googledirectory.jar

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

A connection string for Google Directory will typically look like the following:

jdbc:googledirectory:OAuthClientId=MyOAuthClientId;OAuthClientSecret=MyOAuthClientSecret;CallbackURL=http://localhost;InitiateOAuth=GETANDREFRESH

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:googledirectory:OAuthClientId=MyOAuthClientId;OAuthClientSecret=MyOAuthClientSecret;CallbackURL=http://localhost;InitiateOAuth=GETANDREFRESH"); MyMyTableDAO dao = dbi.open(MyMyTableDAO.class); //do stuff with the DAO dao.close();

Read Google Directory Data

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

//disply the result of our 'find' method String description = dao.findDescriptionByStatus("confirmed"); System.out.println(description);

Write Google Directory Data

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

//add a new entry to the MyTable entity dao.insert(newStatus, newDescription);

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