Discover how a bimodal integration strategy can address the major data management challenges facing your organization today.
Get the Report →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 and write 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 {
//insert new data into Airtable
@SqlUpdate("INSERT INTO SampleTable_1 (Column2, Column1) values (:column2, :column1)")
void insert(@Bind("column2") String column2, @Bind("column1") String column1);
//request specific data from Airtable (String type is used for simplicity)
@SqlQuery("SELECT Column1 FROM SampleTable_1 WHERE Column2 = :column2")
String findColumn1ByColumn2(@Bind("column2") String column2);
/*
* 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=Table1,...;ViewNames=Table1.View1,...;
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=Table1,...;ViewNames=Table1.View1,...;");
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.findColumn1ByColumn2("SomeValue");
System.out.println(column1);
Write Airtable Data
It is also simple to write data to Airtable, using the previously defined method.
//add a new entry to the SampleTable_1 entity
dao.insert(newColumn2, newColumn1);
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.