Ready to get started?

Learn more about Sage US Connectivity Solutions

Learn More

Create a Data Access Object for Sage US Data using JDBI

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

Create a DAO for the Sage US Customer 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 MyCustomerDAO { //insert new data into Sage US @SqlUpdate("INSERT INTO Customer (Name, LastInvoiceAmount) values (:name, :lastInvoiceAmount)") void insert(@Bind("name") String name, @Bind("lastInvoiceAmount") String lastInvoiceAmount); //request specific data from Sage US (String type is used for simplicity) @SqlQuery("SELECT LastInvoiceAmount FROM Customer WHERE Name = :name") String findLastInvoiceAmountByName(@Bind("name") String name); /* * close with no args is used to close the connection */ void close(); }

Open a Connection to Sage US

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

The Application Id and Company Name connection string options are required to connect to Sage as a data source. You can obtain an Application Id by contacting Sage directly to request access to the Sage 50 SDK.

Sage must be installed on the machine. The Sage.Peachtree.API.dll and Sage.Peachtree.API.Resolver.dll assemblies are required. These assemblies are installed with Sage in C:\Program Files\Sage\Peachtree\API\. Additionally, the Sage SDK requires .NET Framework 4.0 and is only compatible with 32-bit applications. To use the Sage SDK in Visual Studio, set the Platform Target property to "x86" in Project -> Properties -> Build.

You must authorize the application to access company data: To authorize your application to access Sage, restart the Sage application, open the company you want to access, and connect with your application. You will then be prompted to set access permissions for the application in the resulting dialog.

While the compiled executable will require authorization only once, during development you may need to follow this process to reauthorize a new build. To avoid restarting the Sage application when developing with Visual Studio, click Build -> Configuration Manager and uncheck "Build" for your project.

Built-in Connection String Designer

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

java -jar cdata.jdbc.sage50us.jar

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

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

jdbc:sage50us:ApplicationId=8dfafu4V4ODmh1fM0xx;CompanyName=Bellwether Garden Supply - Premium;

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:sage50us:ApplicationId=8dfafu4V4ODmh1fM0xx;CompanyName=Bellwether Garden Supply - Premium;"); MyCustomerDAO dao =; //do stuff with the DAO dao.close();

Read Sage US Data

With the connection open to Sage US, simply call the previously defined method to retrieve data from the Customer entity in Sage US.

//disply the result of our 'find' method String lastInvoiceAmount = dao.findLastInvoiceAmountByName("ALDRED"); System.out.println(lastInvoiceAmount);

Write Sage US Data

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

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

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