Create a Data Access Object for Salesforce Einstein Data using JDBI

Ready to get started?

Download for a free trial:

Download Now

Learn more:

Einstein JDBC Driver

Rapidly create and deploy powerful Java applications that integrate with Salesforce Einstein.

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

Create a DAO for the Salesforce Einstein 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 Einstein @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 Einstein (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 Einstein

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

Salesforce Einstein Analytics uses the OAuth 2 authentication standard. You will need to obtain the OAuthClientId and OAuthClientSecret by registering an app with Salesforce Einstein Analytics.

See the Getting Started section of the CData data provider documentation for an authentication guide.

Built-in Connection String Designer

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

java -jar cdata.jdbc.sfeinsteinanalytics.jar

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

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


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:sfeinsteinanalytics:OAuthClientId=MyConsumerKey;OAuthClientSecret=MyConsumerSecret;CallbackURL=http://localhost:portNumber;InitiateOAuth=GETANDREFRESH"); MyDataset_OpportunityDAO dao =; //do stuff with the DAO dao.close();

Read Salesforce Einstein Data

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

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

Write Salesforce Einstein Data

It is also simple to write data to Salesforce Einstein, 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 Einstein by integrating with the CData JDBC Driver for Salesforce Einstein. Download a free trial and work with live Salesforce Einstein data in custom Java applications today.