Spring Boot Oracle Connection Example

In this quick tutorial, we show how to connect the Spring boot application with the Oracle database.

We assume that you have installed the Oracle database on your machine.

Steps to Connect Spring boot application to Oracle Database

Step1: Add Oracle JDBC Driver Dependency

Open your Spring boot application pom.xml file and add the below Oracle JDBC driver dependency in it:
<dependency>
    <groupId>com.oracle.database.jdbc</groupId>
    <artifactId>ojdbc8</artifactId>
    <scope>runtime</scope>
</dependency>
Here, I use ojdbc8 for JDK 8 with Oracle database 11g and 12c. For Oracle Database 18c and 19c, use the artifactId ojdbc10.

Step 2: Connect to Oracle Database with Spring Data JPA

Let's add Spring Data JPA starter dependency to talk with the Oracle database. Open the pom.xml file and add the following dependency to it:
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

Step 3: Configure Oracle Database in Spring Boot Application

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

# Datasource properties
spring.datasource.url=jdbc:oracle:thin:@localhost:1521:xe
spring.datasource.username=username
spring.datasource.password=password

# Hibernate properties
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
Here, the JDBC URL points to an instance of an Oracle database server running on localhost:
spring.datasource.url=jdbc:oracle:thin:@localhost:1521:xe

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

spring.datasource.username=username
spring.datasource.password=password

We have added Hibernate dialect for the Oracle database:

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

Comments