Create a Data Access Object for SAP Fieldglass Data using JDBI



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

Create a DAO for the SAP Fieldglass AuditTrails 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 MyAuditTrailsDAO { //request specific data from SAP Fieldglass (String type is used for simplicity) @SqlQuery("SELECT Category FROM AuditTrails WHERE Company = :company") String findCategoryByCompany(@Bind("company") String company); /* * close with no args is used to close the connection */ void close(); }

Open a Connection to SAP Fieldglass

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

To authenticate, you will need to specify the Username, Password, APIKey, and EnvironmentURL connection properties.

To obtain an APIKey, log in to the SAP API Business Hub and click on Get API Key.

Built-in Connection String Designer

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

java -jar cdata.jdbc.sapfieldglass.jar

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

A connection string for SAP Fieldglass will typically look like the following:

jdbc:sapfieldglass:EnvironmentURL='https://myinstance.com';Username=myuser;Password=mypassword;APIKey=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx;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:sapfieldglass:EnvironmentURL='https://myinstance.com';Username=myuser;Password=mypassword;APIKey=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx;InitiateOAuth=GETANDREFRESH"); MyAuditTrailsDAO dao = dbi.open(MyAuditTrailsDAO.class); //do stuff with the DAO dao.close();

Read SAP Fieldglass Data

With the connection open to SAP Fieldglass, simply call the previously defined method to retrieve data from the AuditTrails entity in SAP Fieldglass.

//disply the result of our 'find' method String category = dao.findCategoryByCompany("CData"); System.out.println(category);

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

Ready to get started?

Download a free trial of the SAP Fieldglass Driver to get started:

 Download Now

Learn more:

SAP Fieldglass Icon SAP Fieldglass JDBC Driver

Provides Java developers with the power to easily connect their Web, Desktop, and Mobile applications to data in SAP Fieldglass Approvals, Audit Trails, Analytics, and more!