Skip to Main Content

Java and i (Part 1) – How to Connect Spring Boot to the IBM Db2 for i Database

Hand pointing at a laptop screen

Introduction: Connect Spring Boot to the IBM Db2 for i Database

This is the first part of a series of technical articles about Java and IBM i integration, called “Java and i” (yes, some pun intended). We will explore running Java applications on IBM i and leveraging the integrated IBM Db2 for i database.

Initially, we will use Spring Boot and Spring’s ecosystem. Later, we will use Java EE MicroProfile, particularly IBM WebSphere Liberty. In the meantime, we will see how to call IBM i native programs (either Cobol or RPG) from a Java application. In addition, we will also see how to call Java from an RPG program. Expect to have lots of code, commands and to roll up your sleeves.

In this first part, we will start with a simple Spring Boot application that exposes a REST API. The target of this part is to access the Db2 for i database from the developer’s IDE. This is easy enough as a starting point. As this implies, you need to have access to an IBM i machine to test the code and the sample application. Be sure that you can access the Db2 for i instance port from your PC.

On your PC, you will need:

1. Sample REST Application from Spring Guides

So, let’s get started. We will download the sample REST application from Spring Guides. This site contains a substantial list of guides and samples for learning Spring Boot and its ecosystem.

For our purpose, we will use this guide: Accessing JPA Data with REST. You can either go through the whole guide, or take a shortcut and just download the REST application or clone it with Git. Go to the gs-accessing-data-rest/complete folder and import this Maven project into your IDE. The project should look like this in IntelliJ:

File screenshot

Run the ApplicationTests test class. And the results should all be green and successful.

File screenshot

This application uses an in-memory relational database, called H2. Next, we’re going to switch the connection to IBM Db2 for i database.

2. Apply changes in IBM Db2 for i database

2.1. Create a schema for testing purposes

We will create a schema called BOOTTEST on Db2 for i. The easiest way I know is to use “Run SQL Scripts” on the IBM i Access Client Solutions desktop application, or maybe ask a DBA to do it. Create the BOOTTEST schema:

create schema BOOTTEST;

The BOOTTEST library is then created on the IBM i. It is also journaled and an SQL catalog is defined. The library should look like this:

Code screenshot

2.2. Create a table in the schema

Using the same “Run SQL Scripts”, create the TPERSON table in the BOOTTEST schema:

  ID         int generated always as identity
                 constraint TPERSON_PK primary key,
  FIRST_NAME varchar(100),
  LAST_NAME  varchar(100)

3. Change the Java code in the IDE

3.1. Change the Person class

Insert the table annotation with TPERSON as table name and BOOTTEST as schema. Change the generation type of the @Id from AUTO to IDENTITY. Notice that for the TPERSON table definition, the ID column is “generated always as identity”. The Person class should begin with these lines:

package hello;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Table(name = "TPERSON", schema = "BOOTTEST")
public class Person {

@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;

The remaining class stays the same.

3.2. Change Maven’s pom.xml

To use the Spring Boot latest release, we could change its version to 2.1.3.RELEASE (as of this writing):


Instead of using H2 in-memory database, we want to use the IBM Db2 for i database. The JDBC driver for IBM Db2 for i can be found on the JTOpen site. The latest JAR could also be found on a Maven repository.





3.3. Add an application configuration file

The Spring Boot standard configuration file is either application.yml or We want to put the IBM Db2 for i database location and driver class in the configuration file. I prefer using the application.yml file:

  url: jdbc:as400://[ibm_i_ip_address];naming=system;libraries=BOOTTEST;socket
timeout=30000;thread used=false;transaction isolation=read commited;translate
binary=true;date format=iso;prompt=false
  username: [db2_for_i_database_username]
  hikari.connection-test-query: values 1

  database-platform: org.hibernate.dialect.DB2400Dialect
  hibernate.ddl-auto: none

The whole URL value should be contained in one line. The displayed text may be displayed on several lines for formatting purposes. The URL JDBC properties (libraries, thread used, transaction isolation, etc.) are defined on the IBM Toolbox for Java JDBC properties page.

Notice that the IBM Db2 for i database password is not defined in the configuration file. This secret value has to be hidden from the source code. We will see below how we can configure the password for testing purposes.

4. Running the code

For our simple purpose, we could put the password in the user’s environment variable. As defined by Spring Boot, this variable should be called SPRING_DATASOURCE_PASSWORD. But we could also configure it in the IDE. Here is an example of this configuration in IntelliJ: Run menu > Edit Configurations… > JUnit > ApplicationTests

Run/Debug Configuration

Now, run the ApplicationTests test class. And the results should all still be green and successful.

For part 2 of this series, we will run the same Spring Boot application on IBM i. Speak to you soon!

Java and i banner
This site is registered on as a development site. Switch to a production site key to remove this banner.