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 IBM Informix Data using JDBI
A brief overview of creating a SQL Object API for IBM Informix 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 IBM Informix integrates connectivity to live IBM Informix data in Java applications. By pairing these technologies, you gain simple, programmatic access to IBM Informix data. This article walks through building a basic Data Access Object (DAO) and the accompanying code to read and write IBM Informix data.
Create a DAO for the IBM Informix Books 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 MyBooksDAO {
//insert new data into IBM Informix
@SqlUpdate("INSERT INTO Books (Category, Price) values (:category, :price)")
void insert(@Bind("category") String category, @Bind("price") String price);
//request specific data from IBM Informix (String type is used for simplicity)
@SqlQuery("SELECT Price FROM Books WHERE Category = :category")
String findPriceByCategory(@Bind("category") String category);
/*
* close with no args is used to close the connection
*/
void close();
}
Open a Connection to IBM Informix
Collect the necessary connection properties and construct the appropriate JDBC URL for connecting to IBM Informix.
Set the following properties to connect to IBM Informix
- Server: Set this to the name of the server running IBM Informix.
- Port: Set this to the port the IBM Informix server is listening on.
- Database: Set this to the name of the IBM Informix database.
- User: Set this to the username of a user allowed to access the database.
- Password: Set this to the password of a user allowed to access the database.
Built-in Connection String Designer
For assistance in constructing the JDBC URL, use the connection string designer built into the IBM Informix JDBC Driver. Either double-click the JAR file or execute the jar file from the command-line.
java -jar cdata.jdbc.informix.jar
Fill in the connection properties and copy the connection string to the clipboard.
A connection string for IBM Informix will typically look like the following:
jdbc:informix:Server=10.0.1.2;Port=50000;User=admin;Password=admin;Database=test;
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:informix:Server=10.0.1.2;Port=50000;User=admin;Password=admin;Database=test;");
MyBooksDAO dao = dbi.open(MyBooksDAO.class);
//do stuff with the DAO
dao.close();
Read IBM Informix Data
With the connection open to IBM Informix, simply call the previously defined method to retrieve data from the Books entity in IBM Informix.
//disply the result of our 'find' method
String price = dao.findPriceByCategory("US");
System.out.println(price);
Write IBM Informix Data
It is also simple to write data to IBM Informix, using the previously defined method.
//add a new entry to the Books entity
dao.insert(newCategory, newPrice);
Since the JDBI library is able to work with JDBC connections, you can easily produce a SQL Object API for IBM Informix by integrating with the CData JDBC Driver for IBM Informix. Download a free trial and work with live IBM Informix data in custom Java applications today.