Ready to get started?

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

Download Now

Create a Data Access Object for USPS Data using JDBI

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

Create a DAO for the USPS 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 USPS @SqlUpdate("INSERT INTO Senders (SenderID, Phone) values (:senderID, :phone)") void insert(@Bind("senderID") String senderID, @Bind("phone") String phone); //request specific data from USPS (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 USPS

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

To authenticate with USPS, set the following connection properties.

  • PostageProvider: The postage provider to use to process requests. Available options are ENDICIA and STAMPS. If unspecified, this property will default to ENDICIA.
  • UseSandbox: This controls whether live or test requests are sent to the production or sandbox servers. If set to true, the Password, AccountNumber, and StampsUserId properties are ignored.
  • StampsUserId: This value is used for logging into authentication to the Stamps servers. This value is not applicable for Endicia and is optional if UseSandbox is true.
  • Password: This value is used for logging into Endicia and Stamps servers. If the postage provider is Endicia, this will be the pass phrase associated with your postage account. It is optional if UseSandbox is true.
  • AccountNumber: The shipper's account number. It is optional if UseSandbox is true.
  • PrintLabelLocation: This property is required to use the GenerateLabels or GenerateReturnLabels stored procedures. This should be set to the folder location where generated labels should be stored.

The Cache Database

Many of the useful task available from USPS require a lot of data. To ensure this data is easy to input and recall later, utilize a cache database to make requests. Set the cache connection properties in order to use the cache:

  • CacheLocation: The path to the cache location, for which a connection will be configured with the default cache provider. For example, C:\users\username\documents\uspscache

As an alternative to CacheLocation, set the combination of CacheConnection and CacheProvider to configure a cache connection using a provider separate from the default.

Built-in Connection String Designer

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

java -jar cdata.jdbc.usps.jar

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

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

jdbc:usps:PostageProvider=ENDICIA; RequestId=12345; Password='abcdefghijklmnopqr'; AccountNumber='12A3B4C'

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:usps:PostageProvider=ENDICIA; RequestId=12345; Password='abcdefghijklmnopqr'; AccountNumber='12A3B4C'"); MySendersDAO dao = dbi.open(MySendersDAO.class); //do stuff with the DAO dao.close();

Read USPS Data

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

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

Write USPS Data

It is also simple to write data to USPS, 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 USPS by integrating with the CData JDBC Driver for USPS. Download a free trial and work with live USPS data in custom Java applications today.