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:
       <dependency>
            <groupId>com.ibm.db2.jcc</groupId>
            <artifactId>db2jcc4</artifactId>
            <version>10.1</version>
        </dependency>
Make sure that you have also added Spring Data JPA starter dependency to talk with the DB2 database:
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

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:

# ===============================
# = DATA SOURCE
# ===============================
# Set here configurations for the database connection
spring.datasource.url=jdbc:db2://localhost:50000/EXAMPLE
spring.datasource.username=db2inst1
spring.datasource.password=db2inst1-pwd
spring.datasource.driver-class-name=com.ibm.db2.jcc.DB2Driver
# Keep the connection alive if idle for a long time (needed in production)
spring.datasource.testWhileIdle=true
spring.datasource.validationQuery=SELECT 1
# ===============================
# = JPA / HIBERNATE
# ===============================
# Show or not log for each sql query
spring.jpa.show-sql=true
# Hibernate ddl auto (create, create-drop, update): with "create-drop" the database
# schema will be automatically created afresh for every start of application
spring.jpa.hibernate.ddl-auto=create-drop
# Naming strategy
spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyHbmImpl
spring.jpa.hibernate.naming.physical-strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy
# Allows Hibernate to generate SQL optimized for a particular DBMS
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.DB2Dialect

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:

spring.datasource.driver-class-name=com.ibm.db2.jcc.DB2Driver

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

spring.jpa.hibernate.ddl-auto=create-drop

We have added Hibernate dialect for the DB2 database:

spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.DB2Dialect

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


Comments