Create a Data Access Object for X-Cart Data using JDBI

Ready to get started?

Download for a free trial:

Download Now

Learn more:

X-Cart JDBC Driver

Rapidly create and deploy powerful Java applications that integrate with X-Cart.



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

Create a DAO for the X-Cart PROFILES 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 MyPROFILESDAO { //request specific data from X-Cart (String type is used for simplicity) @SqlQuery("SELECT FirstName FROM PROFILES WHERE Language = :language") String findFirstNameByLanguage(@Bind("language") String language); /* * close with no args is used to close the connection */ void close(); }

Open a Connection to X-Cart

Collect the necessary connection properties and construct the appropriate JDBC URL for connecting to X-Cart.

An API Key must be created for your XCart Installation. In order to obtain the API Key, you need to install and configure the REST API Module for your X-Cart installation.

  • Login to your X-Cart Installation. Click on My addons on the bottom of the left side-bar navigation panel.
  • Search for REST API Module. Install the Module.
  • After the module has been installed, you need to set it up.
    • Go to the Settings section for the REST API Module.
    • On the settings page specify the API Key (only read)
  • Set the ApiKey and Url to the connection property to connect to data.

Built-in Connection String Designer

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

java -jar cdata.jdbc.xcart.jar

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

A connection string for X-Cart will typically look like the following:

jdbc:xcart:Url=https://example.com/shop;ApiKey=MyApiKey;

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:xcart:Url=https://example.com/shop;ApiKey=MyApiKey;"); MyPROFILESDAO dao = dbi.open(MyPROFILESDAO.class); //do stuff with the DAO dao.close();

Read X-Cart Data

With the connection open to X-Cart, simply call the previously defined method to retrieve data from the PROFILES entity in X-Cart.

//disply the result of our 'find' method String firstName = dao.findFirstNameByLanguage("en"); System.out.println(firstName);

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