Create a Data Access Object for Suadeo Data using JDBI

Jerod Johnson
Jerod Johnson
Director, Technology Evangelism
A brief overview of creating a SQL Object API for Suadeo 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 Suadeo integrates connectivity to live Suadeo data in Java applications. By pairing these technologies, you gain simple, programmatic access to Suadeo data. This article explains how to build a basic Data Access Object (DAO) and the accompanying code to read Suadeo data.

Create a DAO for the Suadeo Customers 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 MyCustomersDAO {
  //request specific data from Suadeo (String type is used for simplicity)
  @SqlQuery("SELECT Name FROM Customers WHERE Status = :status")
  String findNameByStatus(@Bind("status") String status);

  /*
   * close with no args is used to close the connection
   */
  void close();
}

Open a Connection to Suadeo

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

The driver uses the OAuth 2.0 Resource Owner Password Credentials (ROPC) grant to authenticate to Suadeo. Authentication occurs directly using your credentials; there is no browser-based authorization flow or refresh token.

Set the following connection properties:

  • URL: The base URL of your Suadeo instance.
  • User: Your Suadeo username.
  • Password: Your Suadeo password.
  • AuthenticationName: The name identifier for the authentication configuration in your Suadeo instance. Different authentication names can be configured for different environments or use cases.

When you connect, the driver sends your credentials to the Suadeo OAuth token endpoint, receives an access token, and uses it for all subsequent requests. A new access token is obtained automatically when needed during the session.

Built-in Connection String Designer

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

java -jar cdata.jdbc.suadeo.jar

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

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

jdbc:suadeo:URL=https://mysuadeoinstance;User=username;Password=password;AuthenticationName=your_auth_name;

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:suadeo:URL=https://mysuadeoinstance;User=username;Password=password;AuthenticationName=your_auth_name;");
MyCustomersDAO dao = dbi.open(MyCustomersDAO.class);

//do stuff with the DAO

dao.close();

Read Suadeo Data

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

//disply the result of our 'find' method
String name = dao.findNameByStatus("Active");
System.out.println(name);

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

Ready to get started?

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

 Download Now

Learn more:

Suadeo Icon Suadeo JDBC Driver

Rapidly create and deploy powerful Java applications that integrate with Suadeo.