Ready to get started?

Learn more about the CData JDBC Driver for SuiteCRM or download a free trial:

Download Now

Create a Data Access Object for SuiteCRM Data using JDBI

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

Create a DAO for the SuiteCRM Accounts 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 MyAccountsDAO { //insert new data into SuiteCRM @SqlUpdate("INSERT INTO Accounts (Industry, Industry) values (:industry, :industry)") void insert(@Bind("industry") String industry, @Bind("industry") String industry); //request specific data from SuiteCRM (String type is used for simplicity) @SqlQuery("SELECT Industry FROM Accounts WHERE Industry = :industry") String findIndustryByIndustry(@Bind("industry") String industry); /* * close with no args is used to close the connection */ void close(); }

Open a Connection to SuiteCRM

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

The User and Password properties must be set to valid SuiteCRM user credentials. Additionally, specify the URL to the SuiteCRM application, for example http://suite.crm.com.

Note that retrieving SuiteCRM metadata can be expensive. It is advised that you store the metadata locally as described in the Caching Metadata section of the data provider help documentation.

Built-in Connection String Designer

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

java -jar cdata.jdbc.suitecrm.jar

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

A connection string for SuiteCRM will typically look like the following:

jdbc:suitecrm:URL=http://mySuiteCRM.com;User=myUser;Password=myPassword;

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:suitecrm:URL=http://mySuiteCRM.com;User=myUser;Password=myPassword;"); MyAccountsDAO dao = dbi.open(MyAccountsDAO.class); //do stuff with the DAO dao.close();

Read SuiteCRM Data

With the connection open to SuiteCRM, simply call the previously defined method to retrieve data from the Accounts entity in SuiteCRM.

//disply the result of our 'find' method String industry = dao.findIndustryByIndustry("Manufacturing"); System.out.println(industry);

Write SuiteCRM Data

It is also simple to write data to SuiteCRM, using the previously defined method.

//add a new entry to the Accounts entity dao.insert(newIndustry, newIndustry);

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