Connect to Sage US Data in JRuby

Create a simple JRuby app with access to live Sage US data.

JRuby is a high-performance, stable, fully threaded Java implementation of the Ruby programming language. The CData JDBC Driver for Sage US makes it easy to integrate connectivity to live Sage US data in JRuby. This article shows how to create a simple JRuby app that connects to Sage US data, executes a query, and displays the results.

Configure a JDBC Connection to Sage US Data

Before creating the app, note the installation location for the JAR file for the JDBC Driver (typically C:\Program Files\CData\CData JDBC Driver for Sage US\lib).

JRuby natively supports JDBC, so you can easily connect to Sage US and execute SQL queries. Initialize the JDBC connection with the getConnection function of the java.sql.DriverManager class.

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.

Below is a typical JDBC connection string for Sage US:

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

Create a JRuby App with Connectivity to Sage US Data

Create a new Ruby file (for example: Sage50USSelect.rb) and open it in a text editor. Copy the following code into your file:

require 'java' require 'rubygems' require 'C:/Program Files/CData/CData JDBC Driver for Sage US 2018/lib/cdata.jdbc.sage50us.jar' url = "jdbc:sage50us:ApplicationId=8dfafu4V4ODmh1fM0xx;CompanyName=Bellwether Garden Supply - Premium;" conn = java.sql.DriverManager.getConnection(url) stmt = conn.createStatement rs = stmt.executeQuery("SELECT Name, LastInvoiceAmount FROM Customer") while (rs.next) do puts rs.getString(1) + ' ' + rs.getString(2) end

With the file completed, you are ready to display your Sage US data with JRuby. To do so, simply run your file from the command line:

jruby -S Sage50USSelect.rb

Writing SQL-92 queries to Sage US allows you to quickly and easily incorporate Sage US data into your own JRuby applications. Download a free trial today!