Spring Boot DB2 Connection Example

In this tutorial, we will show how to connect the Spring boot application to the DB2 database.

DB2 is a database product from IBM. It is a Relational Database Management System (RDBMS). DB2 is designed to store, analyze and retrieve the data efficiently. DB2 product is extended with the support of Object-Oriented features and non-relational structures with XML.

We assume that you have installed the DB2 server on your machine.

Steps to Connect Spring Boot Application to DB2 Database

Step 1:  Create Database

Use the below command to create a database in the DB2 server:

create database EXAMPLE
Note that our database name is EXAMPLE.

Step2: Add DB2 Driver Dependency

Open your Spring boot application pom.xml file and add the below DB2 JDBC driver dependency in it:
Make sure that you have also added Spring Data JPA starter dependency to talk with the DB2 database:

Step 3: Configure DB2 database in Spring Boot Application

Next, we need to configure DB2 database details in our Spring boot application. Open the application.properties file and add the following configuration to it:

# ===============================
# ===============================
# Set here configurations for the database connection
# Keep the connection alive if idle for a long time (needed in production)
spring.datasource.validationQuery=SELECT 1
# ===============================
# ===============================
# Show or not log for each sql query
# Hibernate ddl auto (create, create-drop, update): with "create-drop" the database
# schema will be automatically created afresh for every start of application
# Naming strategy
# Allows Hibernate to generate SQL optimized for a particular DBMS

Make sure that you have changed the database username and password as per your DB2 installation on your machine.

We have added DB2 JDBC driver class:


This property will create and drop a database every start and stop of the Spring boot application:


We have added Hibernate dialect for the DB2 database:


Complete Source code on GitHub

You can find the complete Spring boot project that uses the DB2 database on GitHub at https://github.com/sourcecodeexamples/spring-boot-db2-example

Free Spring Boot Tutorial - 5 Hours Full Course

Watch this course on YouTube at Spring Boot Tutorial | Fee 5 Hours Full Course