Ready to get started?

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

 Download Now

Learn more:

Azure Storage Icon Azure JDBC Driver

Rapidly create and deploy powerful Java applications that integrate with live Azure Table Storage data!

Create a Data Access Object for Azure Table Data using JDBI



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

Create a DAO for the Azure Table NorthwindProducts 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 MyNorthwindProductsDAO { //insert new data into Azure Table @SqlUpdate("INSERT INTO NorthwindProducts (ShipCity, Price) values (:shipCity, :price)") void insert(@Bind("shipCity") String shipCity, @Bind("price") String price); //request specific data from Azure Table (String type is used for simplicity) @SqlQuery("SELECT Price FROM NorthwindProducts WHERE ShipCity = :shipCity") String findPriceByShipCity(@Bind("shipCity") String shipCity); /* * close with no args is used to close the connection */ void close(); }

Open a Connection to Azure Table

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

Specify your AccessKey and your Account to connect. Set the Account property to the Storage Account Name and set AccessKey to one of the Access Keys. Either the Primary or Secondary Access Keys can be used. To obtain these values, navigate to the Storage Accounts blade in the Azure portal. You can obtain the access key by selecting your account and clicking Access Keys in the Settings section.

Built-in Connection String Designer

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

java -jar cdata.jdbc.azuretables.jar

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

A connection string for Azure Table will typically look like the following:

jdbc:azuretables:AccessKey=myAccessKey;Account=myAccountName;

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:azuretables:AccessKey=myAccessKey;Account=myAccountName;"); MyNorthwindProductsDAO dao = dbi.open(MyNorthwindProductsDAO.class); //do stuff with the DAO dao.close();

Read Azure Table Data

With the connection open to Azure Table, simply call the previously defined method to retrieve data from the NorthwindProducts entity in Azure Table.

//disply the result of our 'find' method String price = dao.findPriceByShipCity("New York"); System.out.println(price);

Write Azure Table Data

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

//add a new entry to the NorthwindProducts entity dao.insert(newShipCity, newPrice);

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