Ready to get started?

Download a free trial of the Google Sheets Driver to get started:

 Download Now

Learn more:

Google Sheets Icon Google Sheets JDBC Driver

Easily connect Java applications with real-time data from spreadsheets stored in Google Docs. Use Google Sheets to manage the data that powers your applications.

Create a Data Access Object for Google Sheets Data using JDBI

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

Create a DAO for the Google Sheets Orders 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 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(); }

Open a Connection to 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.

Built-in Connection String Designer

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();

Read 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);

Write 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.