Create a Data Access Object for Redis Data using JDBI

Ready to get started?

Download for a free trial:

Download Now

Learn more:

Redis JDBC Driver

Rapidly create and deploy powerful Java applications that integrate with Redis data stores.

A brief overview of creating a SQL Object API for Redis 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 Redis integrates connectivity to live Redis data in Java applications. By pairing these technologies, you gain simple, programmatic access to Redis data. This article walks through building a basic Data Access Object (DAO) and the accompanying code to read and write Redis data.

Create a DAO for the Redis 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 { //insert new data into Redis @SqlUpdate("INSERT INTO Customers (Country, CompanyName) values (:country, :companyName)") void insert(@Bind("country") String country, @Bind("companyName") String companyName); //request specific data from Redis (String type is used for simplicity) @SqlQuery("SELECT CompanyName FROM Customers WHERE Country = :country") String findCompanyNameByCountry(@Bind("country") String country); /* * close with no args is used to close the connection */ void close(); }

Open a Connection to Redis

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

Set the following connection properties to connect to a Redis instance:

  • Server: Set this to the name or address of the server your Redis instance is running on. You can specify the port in Port.
  • Password: Set this to the password used to authenticate with a password-protected Redis instance , using the Redis AUTH command.

Set UseSSL to negotiate SSL/TLS encryption when you connect.

Built-in Connection String Designer

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

java -jar cdata.jdbc.redis.jar

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

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


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:redis:Server=;Port=6379;Password=myPassword;"); MyCustomersDAO dao =; //do stuff with the DAO dao.close();

Read Redis Data

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

//disply the result of our 'find' method String companyName = dao.findCompanyNameByCountry("US"); System.out.println(companyName);

Write Redis Data

It is also simple to write data to Redis, using the previously defined method.

//add a new entry to the Customers entity dao.insert(newCountry, newCompanyName);

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