Create a Data Access Object for EmailOctopus Data using 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 EmailOctopus integrates connectivity to live EmailOctopus data in Java applications. By pairing these technologies, you gain simple, programmatic access to EmailOctopus data. This article explains how to build a basic Data Access Object (DAO) and the accompanying code to read EmailOctopus data.
Create a DAO for the EmailOctopus CampaignReportBounced 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 MyCampaignReportBouncedDAO {
//request specific data from EmailOctopus (String type is used for simplicity)
@SqlQuery("SELECT ContactId FROM CampaignReportBounced WHERE ContactStatus = :contactStatus")
String findContactIdByContactStatus(@Bind("contactStatus") String contactStatus);
/*
* close with no args is used to close the connection
*/
void close();
}
Open a Connection to EmailOctopus
Collect the necessary connection properties and construct the appropriate JDBC URL for connecting to EmailOctopus.
Start by setting the Profile connection property to the location of the EmailOctopus Profile on disk (e.g. C:\profiles\EmailOctopus.apip). Next, set the ProfileSettings connection property to the connection string for EmailOctopus (see below).
EmailOctopus API Profile Settings
Sign into your EmailOctopus account and navigate to Integrations & API > API > Your API Keys to generate or retrieve your API key.
Built-in Connection String Designer
For assistance in constructing the JDBC URL, use the connection string designer built into the EmailOctopus JDBC Driver. Either double-click the JAR file or execute the jar file from the command-line.
java -jar cdata.jdbc.api.jar
Fill in the connection properties and copy the connection string to the clipboard.
A connection string for EmailOctopus will typically look like the following:
jdbc:api:Profile=C:\profiles\EmailOctopus.apip;ProfileSettings='APIKey=your_api_key';
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:api:Profile=C:\profiles\EmailOctopus.apip;ProfileSettings='APIKey=your_api_key';");
MyCampaignReportBouncedDAO dao = dbi.open(MyCampaignReportBouncedDAO.class);
//do stuff with the DAO
dao.close();
Read EmailOctopus Data
With the connection open to EmailOctopus, simply call the previously defined method to retrieve data from the CampaignReportBounced entity in EmailOctopus.
//disply the result of our 'find' method
String contactId = dao.findContactIdByContactStatus("bounced");
System.out.println(contactId);
Since the JDBI library is able to work with JDBC connections, you can easily produce a SQL Object API for EmailOctopus by integrating with the CData JDBC Driver for EmailOctopus. Download a free trial and work with live EmailOctopus data in custom Java applications today.