JDBC in Spring Boot applications

You can use Spring Data JDBC to implement JDBC based repositories. It allows you to access DB2 and other data sources from your Spring Boot application.

Spring Data JDBC is conceptually simpler than JPA, for more information on how it differs from JPA, see: Reference documentation in the Spring Boot documentation. To use JDBC in your Spring Boot application, add a JDBC artifact to your dependencies in your Spring Boot application to make the necessary Java libraries available. For example, in Maven:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>

Or in Gradle, (implementation"org.springframework.boot:spring-boot-starter-data-jdbc")

To use JDBC in a Spring Boot application, you can define a Liberty dataSource in the server.xml just as you would if using JDBC in a Java EE application. This data source can then be located by using a JNDI lookup that references the jndiName attribute on the dataSource element, and then used by the Spring Boot JdbcTemplate object by using one of the following methods:

  1. Performing a JNDI lookup of the data source in an @Bean annotated dataSource() method and returning the data source.
  2. Naming the data source in the spring.datasource.jndi-name in the Spring application properties. Spring Boot creates the JdbcTemplate using the data source that is named in application.properties.
Note: In option 2, it is also possible to configure all the data source attributes necessary to connect a Spring application to the required data source from within the application.properties file. The attributes are all defined in Common application properties in the Spring Boot documentation. However, this ties the application directly the data source and using JNDI is a more flexible approach.