Ready to get started?

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

Download Now

Create a Data Access Object for Airtable Data using JDBI

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

Create a DAO for the Airtable SampleTable_1 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 MySampleTable_1DAO { //request specific data from Airtable (String type is used for simplicity) @SqlQuery("SELECT Column1 FROM SampleTable_1 WHERE Column1 = :column1") String findColumn1ByColumn1(@Bind("column1") String column1); /* * close with no args is used to close the connection */ void close(); }

Open a Connection to Airtable

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

APIKey, BaseId and TableNames parameters are required to connect to Airtable. ViewNames is an optional parameter where views of the tables may be specified.

  • APIKey : API Key of your account. To obtain this value, after logging in go to Account. In API section click Generate API key.
  • BaseId : Id of your base. To obtain this value, it is in the same section as the APIKey. Click on Airtable API, or navigate to https://airtable.com/api and select a base. In the introduction section you can find "The ID of this base is appxxN2ftedc0nEG7."
  • TableNames : A comma separated list of table names for the selected base. These are the same names of tables as found in the UI.
  • ViewNames : A comma separated list of views in the format of (table.view) names. These are the same names of the views as found in the UI.

Built-in Connection String Designer

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

java -jar cdata.jdbc.airtable.jar

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

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

jdbc:airtable:APIKey=keymz3adb53RqsU;BaseId=appxxN2fe34r3rjdG7;TableNames=TableA,...;ViewNames=TableA.ViewA,...;

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:airtable:APIKey=keymz3adb53RqsU;BaseId=appxxN2fe34r3rjdG7;TableNames=TableA,...;ViewNames=TableA.ViewA,...;"); MySampleTable_1DAO dao = dbi.open(MySampleTable_1DAO.class); //do stuff with the DAO dao.close();

Read Airtable Data

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

//disply the result of our 'find' method String column1 = dao.findColumn1ByColumn1("Value1"); System.out.println(column1);

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