Ready to get started?

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

Download Now

Create a Data Access Object for Sugar CRM Data using JDBI

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

Create a DAO for the Sugar CRM 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 Sugar CRM @SqlUpdate("INSERT INTO Accounts (Name, AnnualRevenue) values (:name, :annualRevenue)") void insert(@Bind("name") String name, @Bind("annualRevenue") String annualRevenue); //request specific data from Sugar CRM (String type is used for simplicity) @SqlQuery("SELECT AnnualRevenue FROM Accounts WHERE Name = :name") String findAnnualRevenueByName(@Bind("name") String name); /* * close with no args is used to close the connection */ void close(); }

Open a Connection to Sugar CRM

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

The User and Password properties, under the Authentication section, must be set to valid SugarCRM user credentials. This will use the default OAuth token created to allow client logins. OAuthClientId and OAuthClientSecret are required if you do not wish to use the default OAuth token.

You can generate a new OAuth consumer key and consumer secret in Admin -> OAuth Keys. Set the OAuthClientId to the OAuth consumer key. Set the OAuthClientSecret to the consumer secret.

Additionally, specify the URL to the SugarCRM account.

Note that retrieving SugarCRM metadata can be expensive. It is advised that you store the metadata locally as described in the "Caching Metadata" chapter of the help documentation.

Built-in Connection String Designer

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

java -jar cdata.jdbc.sugarcrm.jar

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

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

jdbc:sugarcrm:User=MyUser;Password=MyPassword;URL=MySugarCRMAccountURL;CacheMetadata=True;

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:sugarcrm:User=MyUser;Password=MyPassword;URL=MySugarCRMAccountURL;CacheMetadata=True;"); MyAccountsDAO dao = dbi.open(MyAccountsDAO.class); //do stuff with the DAO dao.close();

Read Sugar CRM Data

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

//disply the result of our 'find' method String annualRevenue = dao.findAnnualRevenueByName("Bob"); System.out.println(annualRevenue);

Write Sugar CRM Data

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

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

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