Dhanushka Sampath
Dhanushka Sampath

Reputation: 255

How to connect to oracle database using spring boot

I'm working with Spring Boot application and trying to access an Oracle database. Although it was successfully built, it gives the error below when I am trying to deploy in Kubernetes.

I changed the application.properties file and pom.xml file with below configurations:

Application.yml

 spring.datasource.url=jdbc:oracle:thin:@<IP>:1521:orcl
 spring.datasource.username=<username>
 spring.datasource.password=<password>
 spring.datasource.driver.class=oracle.jdbc.driver.OracleDriver

POM file

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

Exception

***************************
APPLICATION FAILED TO START
***************************
 Description:
 Failed to bind properties under '' to com.zaxxer.hikari.HikariDataSource:
     Property: driverclassname
    Value: oracle.jdbc.OracleDriver
    Origin: "driverClassName" from property source "source"
    Reason: Failed to load driver class oracle.jdbc.OracleDriver in either of 
            HikariConfig class loader or Thread context classloader
 Action:
 Update your application's configuration   

Upvotes: 8

Views: 100804

Answers (9)

Shrip
Shrip

Reputation: 181

I used implementation 'com.oracle:ojdbc7:12.1.0.2.0 gradle dependency.

Added below in the application.properties.

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

# database details
spring.datasource.url=jdbc:oracle:thin:@//<host>:<port>/<database>
spring.datasource.username=<username>
spring.datasource.password=<password>
spring.datasource.driver.class-name=oracle.jdbc.OracleDriver

You can use different OracleDialect if its not working for you.

For me above code is working without any issue.

Upvotes: 0

Amit Kumar Tiwari
Amit Kumar Tiwari

Reputation: 11

Use this configuration in Application.properties.

 spring.jpa.show-sql=true

spring.h2.console.enabled=true

#Using SID
spring.datasource.url= jdbc:oracle:thin:@localhost:1521:ORCL
spring.datasource.username=SYSTEM   
spring.datasource.password=SYSTEM
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver

#hibernate configs
spring.jpa.database-platform=org.hibernate.dialect.OracleDialect
spring.jpa.hibernate.ddl-auto= update 

Caution:

  1. org.hibernate.dialect.Oracle10gDialect has been deprecated.
  2. You need to check { jdbc:oracle:thin:@localhost:1521:ORCL} if the last part of this DATABASE URL is {ORCL} OR {XE}. SEE THE ATTACHED SCREENSHOT. enter image description here

Upvotes: 0

priyansh490
priyansh490

Reputation: 1

  • You just need to add these two dependencies in your pom.xml file and it will work fine.

     <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-jdbc</artifactId>
     </dependency>
    
     <dependency>
         <groupId>com.oracle.database.jdbc</groupId>
         <artifactId>ojdbc8</artifactId>
         <scope>runtime</scope>
     </dependency>
    

Upvotes: 0

Ritunjay kumar
Ritunjay kumar

Reputation: 477

For Oracle DB,

Maven settings:

<dependency>
    <groupId>com.oracle.ojdbc</groupId>
    <artifactId>ojdbc8</artifactId>
    <version>19.3.0.0</version>
</dependency>

Properties settings:

spring.datasource.url=jdbc:oracle:thin:@172.16.10.12:1521/orcl11
spring.datasource.username=[username]
spring.datasource.password=[password]

Don't use

spring.datasource.driver-class-name=oracle.jdbc.OracleDriver

Upvotes: 0

user12264329
user12264329

Reputation: 11

Update the db driver in the file Application.yml to

spring.datasource.driver-class-name=oracle.jdbc.**driver**.OracleDriver or spring.datasource.driver-class-name=oracle.jdbc.OracleDriver

Upvotes: 1

Abdullah Imran
Abdullah Imran

Reputation: 399

Add below dependency and repository in the pom

<dependency>
        <groupId>com.oracle</groupId>
        <artifactId>ojdbc6</artifactId>
        <version>11.2.0.3</version>
    </dependency>

<repositories>
    <repository>
        <id>codelds</id>
        <url>https://code.lds.org/nexus/content/groups/main-repo</url>
    </repository>
   </repositories>

Also add the following properties in the application.properties

    spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
    spring.datasource.url=jdbc:oracle:thin:@localhost:1521:xe(SID)
    spring.datasource.username=system
    spring.datasource.password=pw

Upvotes: 5

smilyface
smilyface

Reputation: 5513

Maven dependency:

    <dependency>
        <groupId>com.oracle</groupId>
        <artifactId>ojdbc7</artifactId>
        <version>12.1.0</version>
    </dependency>

application.yml file :

# Oracle settings
spring.datasource.url=jdbc:oracle:thin:@localhost:1521:xe
spring.datasource.username=system
spring.datasource.password=password
spring.datasource.driver.class-name=oracle.jdbc.OracleDriver

Note : driver.class-name

Sometimes you may need to add spring.jpa.database-platform=org.hibernate.dialect.Oracle10gDialect to application.yml file (for Oracle 10).

Upvotes: 9

Nirmala
Nirmala

Reputation: 1338

You can check if the SpringBoot app example helps.

Upvotes: 0

Darshan Mehta
Darshan Mehta

Reputation: 30819

You need to download Oracle JDBC driver jar file and add it into your classpath in order for your application to load oracle.jdbc.OracleDriver class.

Driver can be downloaded from here.

Upvotes: 0

Related Questions