JDBI からSage US Data のデータアクセスオブジェクトを作成

JDBI でSage US data 用のSQL オブジェクトAPIを作成する方法を概説します。

JDBI は、Fluent スタイルとSQL オブジェクトスタイルという2つの異なるスタイルAPI を公開する、Java 用のSQL コンビニエンスライブラリです。CData JDBC Driver for Sage US は、Java アプリケーションとリアルタイムSage US data のデータ連携を実現します。これらの技術を組み合わせることによって、Sage US data へのシンプルなコードアクセスが可能になります。ここでは、基本的なDAO(Data Access Object )とそれに付随するSage US data の読み書きのためのコードの作成について説明します。

Sage US Customer Entity のDAO を作成

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(); }

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.


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 = dbi.open(MyCustomerDAO.class); //do stuff with the DAO dao.close();

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);

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.