Create a Data Access Object for SAS Data Sets Data using JDBI

Ready to get started?

Download for a free trial:

Download Now

Learn more:

SAS Data Sets JDBC Driver

Rapidly create and deploy powerful Java applications that integrate with SAS Data Sets.



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

Create a DAO for the SAS Data Sets restaurants 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 MyrestaurantsDAO { //insert new data into SAS Data Sets @SqlUpdate("INSERT INTO restaurants (cuisine, borough) values (:cuisine, :borough)") void insert(@Bind("cuisine") String cuisine, @Bind("borough") String borough); //request specific data from SAS Data Sets (String type is used for simplicity) @SqlQuery("SELECT borough FROM restaurants WHERE cuisine = :cuisine") String findboroughBycuisine(@Bind("cuisine") String cuisine); /* * close with no args is used to close the connection */ void close(); }

Open a Connection to SAS Data Sets

Collect the necessary connection properties and construct the appropriate JDBC URL for connecting to SAS Data Sets.

Set the following connection properties to connect to your SAS DataSets files:

  • URI: Set this to the folder containing your .sas7bdat resources. Currently we only support local files.

Built-in Connection String Designer

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

java -jar cdata.jdbc.sasdatasets.jar

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

A connection string for SAS Data Sets will typically look like the following:

jdbc:sasdatasets:URI=C:/myfolder;

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:sasdatasets:URI=C:/myfolder;"); MyrestaurantsDAO dao = dbi.open(MyrestaurantsDAO.class); //do stuff with the DAO dao.close();

Read SAS Data Sets Data

With the connection open to SAS Data Sets, simply call the previously defined method to retrieve data from the restaurants entity in SAS Data Sets.

//disply the result of our 'find' method String borough = dao.findboroughBycuisine("American"); System.out.println(borough);

Write SAS Data Sets Data

It is also simple to write data to SAS Data Sets, using the previously defined method.

//add a new entry to the restaurants entity dao.insert(newcuisine, newborough);

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