Discover how a bimodal integration strategy can address the major data management challenges facing your organization today.
Get the Report →Create a Data Access Object for SharePoint Excel Services Data using JDBI
A brief overview of creating a SQL Object API for SharePoint Excel Services 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 SharePoint Excel Services integrates connectivity to live SharePoint Excel Services data in Java applications. By pairing these technologies, you gain simple, programmatic access to SharePoint Excel Services data. This article walks through building a basic Data Access Object (DAO) and the accompanying code to read and write SharePoint Excel Services data.
Create a DAO for the SharePoint Excel Services Account 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 MyAccountDAO {
//insert new data into SharePoint Excel Services
@SqlUpdate("INSERT INTO Account (Industry, AnnualRevenue) values (:industry, :annualRevenue)")
void insert(@Bind("industry") String industry, @Bind("annualRevenue") String annualRevenue);
//request specific data from SharePoint Excel Services (String type is used for simplicity)
@SqlQuery("SELECT AnnualRevenue FROM Account WHERE Industry = :industry")
String findAnnualRevenueByIndustry(@Bind("industry") String industry);
/*
* close with no args is used to close the connection
*/
void close();
}
Open a Connection to SharePoint Excel Services
Collect the necessary connection properties and construct the appropriate JDBC URL for connecting to SharePoint Excel Services.
The URL, User, and Password properties, under the Authentication section, must be set to valid credentials for SharePoint Online, SharePoint 2010, or SharePoint 2013. Additionally, the Library property must be set to a valid SharePoint Document Library and the File property must be set to a valid .xlsx file in the indicated Library.
Built-in Connection String Designer
For assistance in constructing the JDBC URL, use the connection string designer built into the SharePoint Excel Services JDBC Driver. Either double-click the JAR file or execute the jar file from the command-line.
java -jar cdata.jdbc.excelservices.jar
Fill in the connection properties and copy the connection string to the clipboard.
A connection string for SharePoint Excel Services will typically look like the following:
jdbc:excelservices:URL=https://myorg.sharepoint.com;[email protected];Password=password;File=Book1.xlsx;
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:excelservices:URL=https://myorg.sharepoint.com;[email protected];Password=password;File=Book1.xlsx;");
MyAccountDAO dao = dbi.open(MyAccountDAO.class);
//do stuff with the DAO
dao.close();
Read SharePoint Excel Services Data
With the connection open to SharePoint Excel Services, simply call the previously defined method to retrieve data from the Account entity in SharePoint Excel Services.
//disply the result of our 'find' method
String annualRevenue = dao.findAnnualRevenueByIndustry("Floppy Disks");
System.out.println(annualRevenue);
Write SharePoint Excel Services Data
It is also simple to write data to SharePoint Excel Services, using the previously defined method.
//add a new entry to the Account entity
dao.insert(newIndustry, newAnnualRevenue);
Since the JDBI library is able to work with JDBC connections, you can easily produce a SQL Object API for SharePoint Excel Services by integrating with the CData JDBC Driver for SharePoint Excel Services. Download a free trial and work with live SharePoint Excel Services data in custom Java applications today.