1

Could someone with experience please share recommendations on configuring a db2 database with a Spring Boot App?

Creating a Spring Boot app that will access a db2 table using JpaRepository to render results from query in an HTML view using Thymeleaf.

Looking for a general explanation on how to configure a Spring Boot App that will access a db2 table using Spring Data Jpa. Specifically, what would I need in my build.gradle and application.properties to make this happen?

wallwalker
  • 591
  • 7
  • 14
  • 27
  • I think this can help https://stackoverflow.com/questions/26130148/challenges-starting-off-with-gradle-spring-and-db2 – lak Sep 13 '19 at 16:11

3 Answers3

1

i think this may help you i used it in simple poc


# ===============================
# = 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

and you can read this article may help you here and here and i recommend u the second one

fathy elshemy
  • 481
  • 5
  • 18
1

Please find below required application.properties for DB2. ( Please read the note which actually helps )

spring.datasource.url=jdbc:db2://localhost:6001/TEST
spring.datasource.username=myuser
spring.datasource.password=mypassword
spring.datasource.driver-class-name=com.ibm.db2.jcc.DB2Driver
#spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.DB2390Dialect

Please note:-

  • If you are using with licensed version then you have to include your db2jcc_license_cisuz-x.x.x.jar and db2jcc4-x.x.x.jar
  • Always check your spring.jpa.properties.hibernate.dialect property. For some versions you don't need to mentioned and for some you have to give the respective class (The one i commented given above)
-4

To work with Spring Boot and embedded in-memory database, such as H2, you need just to add its dependency to your project:

<dependency>
   <groupId>com.h2database</groupId>
   <artifactId>h2</artifactId>
   <scope>runtime</scope>-->
</dependency>

and, for example, Spring Data JPA starter:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

Then Spring Boot will make the rest of autoconfiguring itself.

Thus you can start to work with your entities, eg:

@Entity
public class MyEntity {
    @Id
    @GeneratedValue
    private Integer id;

    // other stuff
}

And repositories:

public interface MyEntityRepo extends JpaRepository<MyEntity, Integer> {
}

Additional info:

Cepr0
  • 28,144
  • 8
  • 75
  • 101