![]() |
VOOZH | about |
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 HubDB integrates connectivity to live HubDB data in Java applications. By pairing these technologies, you gain simple, programmatic access to HubDB data. This article explains how to build a basic Data Access Object (DAO) and the accompanying code to read and write HubDB data.
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 HubDB
@SqlUpdate("INSERT INTO NorthwindProducts (Id, Name) values (:id, :name)")
void insert(@Bind("id") String id, @Bind("name") String name);
//request specific data from HubDB (String type is used for simplicity)
@SqlQuery("SELECT Name FROM NorthwindProducts WHERE Id = :id")
String findNameById(@Bind("id") String id);
/*
* close with no args is used to close the connection
*/
void close();
}
Collect the necessary connection properties and construct the appropriate JDBC URL for connecting to HubDB.
There are two authentication methods available for connecting to HubDB data source: OAuth Authentication with a public HubSpot application and authentication with a Private application token.
AuthScheme must be set to "OAuth" in all OAuth flows. Be sure to review the Help documentation for the required connection properties for you specific authentication needs (desktop applications, web applications, and headless machines).
Follow the steps below to register an application and obtain the OAuth client credentials:
Under Scopes, select any scopes you need for your application's intended functionality.
A minimum of the following scopes is required to access tables:
To connect using a HubSpot private application token, set the AuthScheme property to "PrivateApp."
You can generate a private application token by following the steps below:
To connect, set PrivateAppToken to the private application token you retrieved.
For assistance in constructing the JDBC URL, use the connection string designer built into the HubDB JDBC Driver. Either double-click the JAR file or execute the jar file from the command-line.
java -jar cdata.jdbc.hubdb.jar
Fill in the connection properties and copy the connection string to the clipboard.
👁 Using the built-in connection string designer to generate a JDBC URL (Salesforce is shown.)A connection string for HubDB will typically look like the following:
jdbc:hubdb:AuthScheme=OAuth;OAuthClientID=MyOAuthClientID;OAuthClientSecret=MyOAuthClientSecret;CallbackURL=http://localhost:33333;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:hubdb:AuthScheme=OAuth;OAuthClientID=MyOAuthClientID;OAuthClientSecret=MyOAuthClientSecret;CallbackURL=http://localhost:33333;InitiateOAuth=GETANDREFRESH;");
MyNorthwindProductsDAO dao = dbi.open(MyNorthwindProductsDAO.class);
//do stuff with the DAO
dao.close();
With the connection open to HubDB, simply call the previously defined method to retrieve data from the NorthwindProducts entity in HubDB.
//disply the result of our 'find' method
String name = dao.findNameById("1");
System.out.println(name);
It is also simple to write data to HubDB, using the previously defined method.
//add a new entry to the NorthwindProducts entity dao.insert(newId, newName);
Since the JDBI library is able to work with JDBC connections, you can easily produce a SQL Object API for HubDB by integrating with the CData JDBC Driver for HubDB. Download a free trial and work with live HubDB data in custom Java applications today.
Download a free trial of the HubDB Driver to get started:
Download NowLearn more:
👁 HubDB IconRapidly create and deploy powerful Java applications that integrate with HubDB.