Create a Data Access Object for Excel Online Data using JDBI



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

Create a DAO for the Excel Online Test_xlsx_Sheet1 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 MyTest_xlsx_Sheet1DAO { //insert new data into Excel Online @SqlUpdate("INSERT INTO Test_xlsx_Sheet1 (Column2, Column1) values (:column2, :column1)") void insert(@Bind("column2") String column2, @Bind("column1") String column1); //request specific data from Excel Online (String type is used for simplicity) @SqlQuery("SELECT Column1 FROM Test_xlsx_Sheet1 WHERE Column2 = :column2") String findColumn1ByColumn2(@Bind("column2") String column2); /* * close with no args is used to close the connection */ void close(); }

Open a Connection to Excel Online

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

You can connect to a workbook by providing authentication to Excel Online and then setting the following properties:

  • Workbook: Set this to the name or Id of the workbook.

    If you want to view a list of information about the available workbooks, execute a query to the Workbooks view after you authenticate.

  • UseSandbox: Set this to true if you are connecting to a workbook in a sandbox account. Otherwise, leave this blank to connect to a production account.

You use the OAuth authentication standard to authenticate to Excel Online. See the Getting Started section in the help documentation for a guide. Getting Started also guides you through executing SQL to worksheets and ranges.

Built-in Connection String Designer

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

java -jar cdata.jdbc.excelonline.jar

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

A connection string for Excel Online will typically look like the following:

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

Read Excel Online Data

With the connection open to Excel Online, simply call the previously defined method to retrieve data from the Test_xlsx_Sheet1 entity in Excel Online.

//disply the result of our 'find' method String column1 = dao.findColumn1ByColumn2("Bob"); System.out.println(column1);

Write Excel Online Data

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

//add a new entry to the Test_xlsx_Sheet1 entity dao.insert(newColumn2, newColumn1);

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

Ready to get started?

Download a free trial of the Excel Online Driver to get started:

 Download Now

Learn more:

Excel Online Icon Excel Online JDBC Driver

Rapidly create and deploy powerful Java applications that integrate with live Excel Online Spreadsheet data!