Ready to get started?

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

Download Now

Create a Data Access Object for Google Contacts Data using JDBI

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

Create a DAO for the Google Contacts Friends 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 MyFriendsDAO { //insert new data into Google Contacts @SqlUpdate("INSERT INTO Friends (SearchTerms, StartDateTime) values (:searchTerms, :startDateTime)") void insert(@Bind("searchTerms") String searchTerms, @Bind("startDateTime") String startDateTime); //request specific data from Google Contacts (String type is used for simplicity) @SqlQuery("SELECT StartDateTime FROM Friends WHERE SearchTerms = :searchTerms") String findStartDateTimeBySearchTerms(@Bind("searchTerms") String searchTerms); /* * close with no args is used to close the connection */ void close(); }

Open a Connection to Google Contacts

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

You can connect to Google APIs on behalf of individual users or on behalf of a domain. Google uses the OAuth authentication standard. See the "Getting Started" section of the help documentation for a guide.

Built-in Connection String Designer

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

java -jar cdata.jdbc.googlecontacts.jar

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

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

jdbc:googlecontacts: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:googlecontacts:InitiateOAuth=GETANDREFRESH"); MyFriendsDAO dao = dbi.open(MyFriendsDAO.class); //do stuff with the DAO dao.close();

Read Google Contacts Data

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

//disply the result of our 'find' method String startDateTime = dao.findStartDateTimeBySearchTerms("Durham"); System.out.println(startDateTime);

Write Google Contacts Data

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

//add a new entry to the Friends entity dao.insert(newSearchTerms, newStartDateTime);

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