Ready to get started?

Download a free trial of the Square Driver to get started:

 Download Now

Learn more:

Square Icon Square JDBC Driver

Easy-to-use Square client enables Java-based applications to easily consume Square Transactions, Items, Subscriptions, etc.

Create a Data Access Object for Square Data using JDBI



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

Create a DAO for the Square Refunds 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 MyRefundsDAO { //insert new data into Square @SqlUpdate("INSERT INTO Refunds (Type, RefundedMoneyAmount) values (:type, :refundedMoneyAmount)") void insert(@Bind("type") String type, @Bind("refundedMoneyAmount") String refundedMoneyAmount); //request specific data from Square (String type is used for simplicity) @SqlQuery("SELECT RefundedMoneyAmount FROM Refunds WHERE Type = :type") String findRefundedMoneyAmountByType(@Bind("type") String type); /* * close with no args is used to close the connection */ void close(); }

Open a Connection to Square

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

Square uses the OAuth authentication standard. To authenticate using OAuth, you will need to register an app with Square to obtain the OAuthClientId, OAuthClientSecret, and CallbackURL. See the "Getting Started" chapter of the help documentation for a guide to using OAuth.

Additionally, you must specify the LocationId. You can retrieve the Ids for your Locations by querying the Locations table. Alternatively, you can set the LocationId in the search criteria of your query.

Built-in Connection String Designer

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

java -jar cdata.jdbc.square.jar

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

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

jdbc:square:OAuthClientId=MyAppId;OAuthClientSecret=MyAppSecret;CallbackURL=http://localhost:33333;LocationId=MyDefaultLocation;InitiateOAuth=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:square:OAuthClientId=MyAppId;OAuthClientSecret=MyAppSecret;CallbackURL=http://localhost:33333;LocationId=MyDefaultLocation;InitiateOAuth=GETANDREFRESH"); MyRefundsDAO dao = dbi.open(MyRefundsDAO.class); //do stuff with the DAO dao.close();

Read Square Data

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

//disply the result of our 'find' method String refundedMoneyAmount = dao.findRefundedMoneyAmountByType("FULL"); System.out.println(refundedMoneyAmount);

Write Square Data

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

//add a new entry to the Refunds entity dao.insert(newType, newRefundedMoneyAmount);

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