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 Sybase IQ Data using JDBI
A brief overview of creating a SQL Object API for Sybase IQ 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 Sybase IQ integrates connectivity to live Sybase IQ data in Java applications. By pairing these technologies, you gain simple, programmatic access to Sybase IQ data. This article walks through building a basic Data Access Object (DAO) and the accompanying code to read and write Sybase IQ data.
Create a DAO for the Sybase IQ Products 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 MyProductsDAO {
//insert new data into Sybase IQ
@SqlUpdate("INSERT INTO Products (ProductName, Price) values (:productName, :price)")
void insert(@Bind("productName") String productName, @Bind("price") String price);
//request specific data from Sybase IQ (String type is used for simplicity)
@SqlQuery("SELECT Price FROM Products WHERE ProductName = :productName")
String findPriceByProductName(@Bind("productName") String productName);
/*
* close with no args is used to close the connection
*/
void close();
}
Open a Connection to Sybase IQ
Collect the necessary connection properties and construct the appropriate JDBC URL for connecting to Sybase IQ.
To connect to SybaseIQ, set User, Password, Server and Database properties. To secure connections with TLS/SSL, set UseSSL to TRUE.
Built-in Connection String Designer
For assistance in constructing the JDBC URL, use the connection string designer built into the Sybase IQ JDBC Driver. Either double-click the JAR file or execute the jar file from the command-line.
java -jar cdata.jdbc.sybaseiq.jar
Fill in the connection properties and copy the connection string to the clipboard.
A connection string for Sybase IQ will typically look like the following:
jdbc:sybaseiq:User=myuser;Password=mypassword;Server=localhost;Database=Northwind
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:sybaseiq:User=myuser;Password=mypassword;Server=localhost;Database=Northwind");
MyProductsDAO dao = dbi.open(MyProductsDAO.class);
//do stuff with the DAO
dao.close();
Read Sybase IQ Data
With the connection open to Sybase IQ, simply call the previously defined method to retrieve data from the Products entity in Sybase IQ.
//disply the result of our 'find' method
String price = dao.findPriceByProductName("Konbu");
System.out.println(price);
Write Sybase IQ Data
It is also simple to write data to Sybase IQ, using the previously defined method.
//add a new entry to the Products entity
dao.insert(newProductName, newPrice);
Since the JDBI library is able to work with JDBC connections, you can easily produce a SQL Object API for Sybase IQ by integrating with the CData JDBC Driver for Sybase IQ. Download a free trial and work with live Sybase IQ data in custom Java applications today.