Create a Data Access Object for Sage X3 Cloud Data using JDBI

Jerod Johnson
Jerod Johnson
Director, Technology Evangelism
A brief overview of creating a SQL Object API for Sage X3 Cloud 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 Sage X3 Cloud integrates connectivity to live Sage X3 Cloud data in Java applications. By pairing these technologies, you gain simple, programmatic access to Sage X3 Cloud data. This article explains how to build a basic Data Access Object (DAO) and the accompanying code to read Sage X3 Cloud data.

Create a DAO for the Sage X3 Cloud BPCUSTOMER 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 MyBPCUSTOMERDAO {
  //request specific data from Sage X3 Cloud (String type is used for simplicity)
  @SqlQuery("SELECT BPCNAM FROM BPCUSTOMER WHERE BPCNUM = :bPCNUM")
  String findBPCNAMByBPCNUM(@Bind("bPCNUM") String bPCNUM);

  /*
   * close with no args is used to close the connection
   */
  void close();
}

Open a Connection to Sage X3 Cloud

Collect the necessary connection properties and construct the appropriate JDBC URL for connecting to Sage X3 Cloud.

Sage X3 Cloud uses the OAuth 2.0 Client Credentials flow, and an X-API-Key is also required for API access. Set AuthScheme to OAuth and specify the following connection properties:

  • URL: The base URL of your Sage X3 Cloud instance.
  • OAuthAccessTokenUrl: The OAuth token endpoint (e.g., https://your-auth-domain/oauth/token).
  • OAuthClientId: Your OAuth application client ID.
  • OAuthClientSecret: Your OAuth application client secret.
  • Audience: The API audience value for the token request.
  • XAPIKey: The X-API-Key provided by your Sage X3 Cloud administrator.
  • Folder: The Sage X3 folder name (e.g., SEED). This folder is used as the default schema.
  • Folders (optional): A comma-separated list of Sage X3 folders (e.g., SEED,PERF). Each folder is exposed as a separate schema, so you can query across folders with the Schema.Table syntax.

The driver obtains an access token with the Client Credentials flow and sends it with the X-API-Key on every API request. With InitiateOAuth set to GETANDREFRESH (the default), the driver acquires and refreshes the token automatically.

Built-in Connection String Designer

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


java -jar cdata.jdbc.sagex3cloud.jar

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

Using the built-in connection string designer to generate a JDBC URL (Salesforce is shown.)

A connection string for Sage X3 Cloud will typically look like the following:

jdbc:sagex3cloud:AuthScheme=OAuth;URL=https://x3server/;OAuthAccessTokenUrl=https://auth-domain/oauth/token;OAuthClientId=your_client_id;OAuthClientSecret=your_client_secret;Audience=https://api-audience;XAPIKey=your_api_key;Folder=SEED;

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:sagex3cloud:AuthScheme=OAuth;URL=https://x3server/;OAuthAccessTokenUrl=https://auth-domain/oauth/token;OAuthClientId=your_client_id;OAuthClientSecret=your_client_secret;Audience=https://api-audience;XAPIKey=your_api_key;Folder=SEED;");
MyBPCUSTOMERDAO dao = dbi.open(MyBPCUSTOMERDAO.class);

//do stuff with the DAO

dao.close();

Read Sage X3 Cloud Data

With the connection open to Sage X3 Cloud, simply call the previously defined method to retrieve data from the BPCUSTOMER entity in Sage X3 Cloud.


//disply the result of our 'find' method
String bPCNAM = dao.findBPCNAMByBPCNUM("MARTIN");
System.out.println(bPCNAM);

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

Ready to get started?

Download a free trial of the Sage X3 Cloud Driver to get started:

 Download Now

Learn more:

Sage X3 Cloud Icon Sage X3 Cloud JDBC Driver

Rapidly create and deploy powerful Java applications that integrate with Sage X3 Cloud.