Create a Data Access Object for Salesforce CRM Analytics Data using JDBI

Jerod Johnson
Jerod Johnson
Director, Technology Evangelism
A brief overview of creating a SQL Object API for Salesforce CRM Analytics 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 Salesforce CRM Analytics integrates connectivity to live Salesforce CRM Analytics data in Java applications. By pairing these technologies, you gain simple, programmatic access to Salesforce CRM Analytics data. This article explains how to build a basic Data Access Object (DAO) and the accompanying code to read and write Salesforce CRM Analytics data.

Create a DAO for the Salesforce CRM Analytics Dataset_Opportunity 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 MyDataset_OpportunityDAO {
  //insert new data into Salesforce CRM Analytics
  @SqlUpdate("INSERT INTO Dataset_Opportunity (StageName, CloseDate) values (:stageName, :closeDate)")
  void insert(@Bind("stageName") String stageName, @Bind("closeDate") String closeDate);

  //request specific data from Salesforce CRM Analytics (String type is used for simplicity)
  @SqlQuery("SELECT CloseDate FROM Dataset_Opportunity WHERE StageName = :stageName")
  String findCloseDateByStageName(@Bind("stageName") String stageName);

  /*
   * close with no args is used to close the connection
   */
  void close();
}

Open a Connection to Salesforce CRM Analytics

Collect the necessary connection properties and construct the appropriate JDBC URL for connecting to Salesforce CRM Analytics.

Salesforce CRM Analytics uses the OAuth 2 authentication standard. Obtain the OAuthClientId and OAuthClientSecret by registering an app with Salesforce CRM Analytics.

See the Getting Started section of the Help documentation for an authentication guide.

Multi-Factor Authentication (MFA)

If the connected Salesforce org has MFA enforcement enabled, set MFACode to the time-based one-time passcode (TOTP) generated by your authenticator app (such as Salesforce Authenticator or Google Authenticator). MFACode applies alongside the standard OAuth flow.

Built-in Connection String Designer

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

java -jar cdata.jdbc.salesforcecrmanalytics.jar

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

Using the built-in connection string designer to generate a JDBC URL (Salesforce is shown.)

A connection string for Salesforce CRM Analytics will typically look like the following:

jdbc:salesforcecrmanalytics:OAuthClientId=MyConsumerKey;OAuthClientSecret=MyConsumerSecret;CallbackURL=http://localhost:portNumber;InitiateOAuth=GETANDREFRESH;MFACode=YourMFACode

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:salesforcecrmanalytics:OAuthClientId=MyConsumerKey;OAuthClientSecret=MyConsumerSecret;CallbackURL=http://localhost:portNumber;InitiateOAuth=GETANDREFRESH;MFACode=YourMFACode");
MyDataset_OpportunityDAO dao = dbi.open(MyDataset_OpportunityDAO.class);

//do stuff with the DAO

dao.close();

Read Salesforce CRM Analytics Data

With the connection open to Salesforce CRM Analytics, simply call the previously defined method to retrieve data from the Dataset_Opportunity entity in Salesforce CRM Analytics.

//disply the result of our 'find' method
String closeDate = dao.findCloseDateByStageName("Closed Won");
System.out.println(closeDate);

Write Salesforce CRM Analytics Data

It is also simple to write data to Salesforce CRM Analytics, using the previously defined method.

//add a new entry to the Dataset_Opportunity entity
dao.insert(newStageName, newCloseDate);

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

Ready to get started?

Download a free trial of the Salesforce CRM Analytics Driver to get started:

 Download Now

Learn more:

Salesforce CRM Analytics Icon Salesforce CRM Analytics JDBC Driver

Rapidly create and deploy powerful Java applications that integrate with Salesforce CRM Analytics data including Dataset, Lens, and more!