Ready to get started?

Learn more about the CData JDBC Driver for UPS or download a free trial:

Download Now

Create a Data Access Object for UPS Data using JDBI

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

Create a DAO for the UPS Senders 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 MySendersDAO { //insert new data into UPS @SqlUpdate("INSERT INTO Senders (SenderID, Phone) values (:senderID, :phone)") void insert(@Bind("senderID") String senderID, @Bind("phone") String phone); //request specific data from UPS (String type is used for simplicity) @SqlQuery("SELECT Phone FROM Senders WHERE SenderID = :senderID") String findPhoneBySenderID(@Bind("senderID") String senderID); /* * close with no args is used to close the connection */ void close(); }

Open a Connection to UPS

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

The driver uses five pieces of information in order to authenticate its actions with the UPS service.

  • Server: This controls the URL where the requests should be sent. Common testing options for this are: https://wwwcie.ups.com/ups.app/xml and https://wwwcie.ups.com/webservices
  • AccessKey: This is an identifier that is required to connect to a UPS Server. This value will be provided to you by UPS after registration.
  • UserId: This value is used for logging into UPS. This value is the one you chose to login with when registering for service with UPS.
  • Password: This value is used for logging into UPS. This value is the one you chose to login with when registering for service with UPS.
  • AccountNumber: This is a valid 6-digit or 10-digit UPS account number.
  • PrintLabelLocation: This property is required if one intends to use the GenerateLabels or GenerateReturnLabels stored procedures. This should be set to the folder location where generated labels should be stored.

Built-in Connection String Designer

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

java -jar cdata.jdbc.ups.jar

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

A connection string for UPS will typically look like the following:

jdbc:ups:Server=https://wwwcie.ups.com/ups.app/xml;AccessKey=myAccessKey;Password=myPassword;AccountNumber=myAccountNumber;UserId=myUserIdInitiateOAuth=GETANDREFRESH

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:ups:Server=https://wwwcie.ups.com/ups.app/xml;AccessKey=myAccessKey;Password=myPassword;AccountNumber=myAccountNumber;UserId=myUserIdInitiateOAuth=GETANDREFRESH"); MySendersDAO dao = dbi.open(MySendersDAO.class); //do stuff with the DAO dao.close();

Read UPS Data

With the connection open to UPS, simply call the previously defined method to retrieve data from the Senders entity in UPS.

//disply the result of our 'find' method String phone = dao.findPhoneBySenderID("25"); System.out.println(phone);

Write UPS Data

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

//add a new entry to the Senders entity dao.insert(newSenderID, newPhone);

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