Create a Data Access Object for Short.io Data using 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 Short.io integrates connectivity to live Short.io data in Java applications. By pairing these technologies, you gain simple, programmatic access to Short.io data. This article explains how to build a basic Data Access Object (DAO) and the accompanying code to read Short.io data.
Create a DAO for the Short.io Domains 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 MyDomainsDAO {
//request specific data from Short.io (String type is used for simplicity)
@SqlQuery("SELECT FROM Domains WHERE = :")
String findBy(@Bind("") String );
/*
* close with no args is used to close the connection
*/
void close();
}
Open a Connection to Short.io
Collect the necessary connection properties and construct the appropriate JDBC URL for connecting to Short.io.
Using API Key Authentication
Short.io uses API Key authentication. To obtain your API key:
- Log in to your Short.io account
- Navigate to Settings > Integrations & API > API
- Click Create API Key and copy your API key
After obtaining the API key, you are ready to connect:
- AuthScheme: Set this to APIKey.
- APIKey: Set this to your Short.io API key obtained from Settings > Integrations & API > API.
Example connection string:
Profile=C:\profiles\ShortIo.apip;AuthScheme=APIKey;ProfileSettings='APIKey=your_api_key';
Available Tables
The Short.io profile provides access to the following tables:
- Domains - Short.io domains associated with the authenticated account
- Links - Short links for a domain
- LinkExpand - Expand a short link by domain and path
- LinksByOriginalUrl - Retrieve multiple short links matching a given original destination URL
- Folders - Link folders within a specific domain
- LinkPermissions - Permission records for a specific link within a domain
- CountryTargeting - Country-based redirect targeting rules for a specific short link
- RegionTargeting - Region-based redirect targeting rules for a specific short link
- Regions - List of available regions/states for a given country code
- DomainStatistics - Aggregated click and traffic statistics for a Short.io domain
- LinkStatistics - Aggregated click and traffic statistics for a specific Short.io link
Built-in Connection String Designer
For assistance in constructing the JDBC URL, use the connection string designer built into the Short.io JDBC Driver. Either double-click the JAR file or execute the jar file from the command-line.
java -jar cdata.jdbc.api.jar
Fill in the connection properties and copy the connection string to the clipboard.
A connection string for Short.io will typically look like the following:
jdbc:api:Profile=C:\profiles\ShortIo.apip;AuthScheme=APIKey;ProfileSettings='APIKey=your_api_key';
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:api:Profile=C:\profiles\ShortIo.apip;AuthScheme=APIKey;ProfileSettings='APIKey=your_api_key';");
MyDomainsDAO dao = dbi.open(MyDomainsDAO.class);
//do stuff with the DAO
dao.close();
Read Short.io Data
With the connection open to Short.io, simply call the previously defined method to retrieve data from the Domains entity in Short.io.
//disply the result of our 'find' method
String = dao.findBy("");
System.out.println();
Since the JDBI library is able to work with JDBC connections, you can easily produce a SQL Object API for Short.io by integrating with the CData JDBC Driver for Short.io. Download a free trial and work with live Short.io data in custom Java applications today.