Create a Data Access Object for Google Spanner Data using JDBI

Ready to get started?

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

 Download Now

Learn more:

Google Cloud Spanner Icon Google Spanner JDBC Driver

Rapidly create and deploy powerful Java applications that integrate with Google Cloud Spanner databases.

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

Create a DAO for the Google Spanner Customer 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 MyCustomerDAO { //insert new data into Google Spanner @SqlUpdate("INSERT INTO Customer (Id, TotalDue) values (:id, :totalDue)") void insert(@Bind("id") String id, @Bind("totalDue") String totalDue); //request specific data from Google Spanner (String type is used for simplicity) @SqlQuery("SELECT TotalDue FROM Customer WHERE Id = :id") String findTotalDueById(@Bind("id") String id); /* * close with no args is used to close the connection */ void close(); }

Open a Connection to Google Spanner

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

Google Spanner uses the OAuth authentication standard. To authenticate using OAuth, you can use the embedded credentials or register an app with Google.

See the Getting Started guide in the CData driver documentation for more information.

Built-in Connection String Designer

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

java -jar cdata.jdbc.googlespanner.jar

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

A connection string for Google Spanner 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:googlespanner:ProjectId='project1';InstanceId='instance1';Database='db1';InitiateOAuth=GETANDREFRESH"); MyCustomerDAO dao =; //do stuff with the DAO dao.close();

Read Google Spanner Data

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

//disply the result of our 'find' method String totalDue = dao.findTotalDueById("1"); System.out.println(totalDue);

Write Google Spanner Data

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

//add a new entry to the Customer entity dao.insert(newId, newTotalDue);

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