Create a Data Access Object for Smartsheet Data using JDBI

Ready to get started?

Download for a free trial:

Download Now

Learn more:

Smartsheet JDBC Driver

Easy-to-use Smartsheet client enables Java-based applications to easily consume Smartsheet Sheets, Contacts, Folders, Groups, Users, etc.



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

Create a DAO for the Smartsheet Sheet_Event_Plan_Budget 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 MySheet_Event_Plan_BudgetDAO { //insert new data into Smartsheet @SqlUpdate("INSERT INTO Sheet_Event_Plan_Budget (Assigned, Progress) values (:assigned, :progress)") void insert(@Bind("assigned") String assigned, @Bind("progress") String progress); //request specific data from Smartsheet (String type is used for simplicity) @SqlQuery("SELECT Progress FROM Sheet_Event_Plan_Budget WHERE Assigned = :assigned") String findProgressByAssigned(@Bind("assigned") String assigned); /* * close with no args is used to close the connection */ void close(); }

Open a Connection to Smartsheet

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

Smartsheet uses the OAuth authentication standard. To authenticate using OAuth, you will need to register an app to obtain the OAuthClientId, OAuthClientSecret, and CallbackURL connection properties.

However, for testing purposes you can instead use the Personal Access Token you get when you create an application; set this to the OAuthAccessToken connection property.

Built-in Connection String Designer

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

java -jar cdata.jdbc.smartsheet.jar

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

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

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

Read Smartsheet Data

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

//disply the result of our 'find' method String progress = dao.findProgressByAssigned("Ana Trujilo"); System.out.println(progress);

Write Smartsheet Data

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

//add a new entry to the Sheet_Event_Plan_Budget entity dao.insert(newAssigned, newProgress);

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