Ready to get started?

Download a free trial of the Gmail Driver to get started:

 Download Now

Learn more:

Gmail Icon Gmail JDBC Driver

Rapidly create and deploy powerful Java applications that integrate powerful Gmail send and receive capabilities. . Send & Receive Email, manage Gmail folders & Messages, and more!

Create a Data Access Object for Gmail Data using JDBI



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

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

Open a Connection to Gmail

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

There are two ways to authenticate to Gmail. Before selecting one, first ensure that you have enabled IMAP access in your Gmail account settings. See the "Connecting to Gmail" section under "Getting Started" in the installed documentation for a guide.

The User and Password properties, under the Authentication section, can be set to valid Gmail user credentials.

Alternatively, instead of providing the Password, you can use 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, you will need to provide the User. See the "Getting Started" chapter in the help documentation for a guide to using OAuth.

Built-in Connection String Designer

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

java -jar cdata.jdbc.gmail.jar

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

A connection string for Gmail will typically look like the following:

jdbc:gmail:User=username;Password=password;

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:gmail:User=username;Password=password;"); MyInboxDAO dao = dbi.open(MyInboxDAO.class); //do stuff with the DAO dao.close();

Read Gmail Data

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

//disply the result of our 'find' method String size = dao.findSizeByFrom("test@test.com"); System.out.println(size);

Write Gmail Data

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

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

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