How to connect to oracle database using spring boot
Asked Answered
H

9

8

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   
Helpmate answered 22/1, 2019 at 9:44 Comment(3)
You need the OJDBC driverOl
Check my answer on spring.datasource.driver.class=oracle.jdbc.driver.OracleDriver it should be driver.class-namePenelopa
@Dhanushka - If the issue fixed, please select correct answer. It might be useful for others. CheersPenelopa
P
8

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).

Penelopa answered 22/1, 2019 at 9:54 Comment(0)
G
5

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
Galling answered 13/8, 2019 at 10:23 Comment(0)
A
1

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
Aalii answered 23/10, 2019 at 16:16 Comment(3)
Another inconsistency? spring.datasource.driver.class-name v/s spring.datasource.driver-class-nameAalii
Requests for clarification or further information from OP should go in a comment, not an answer. Please only post an answer if it actually answers the question.Selfwinding
By the way, I found the answers to my questions:use spring.datasource.driver-class-name=oracle.jdbc.OracleDriver for >Oracle 9i (no driver needed) However, if I unpack the ojdbc6.jar I see both classes there. And the correct attribute to use is spring.datasource.driver-class-nameAalii
M
0

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.

Millennial answered 22/1, 2019 at 9:47 Comment(2)
I already tried it in InteliJ. It successfully build it in IDE. But when I try to deploy it in kubernetes, it gives the error. please helpHelpmate
Are you creating a fat jar? If yes then you can include driver jar explicitly.Millennial
H
0

You can check if the SpringBoot app example helps.

Hagride answered 23/1, 2019 at 1:17 Comment(0)
S
0

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
Sudiesudnor answered 12/11, 2021 at 11:25 Comment(0)
B
0
  • 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>
    
Brinna answered 13/6, 2022 at 4:47 Comment(0)
L
0

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
Lawyer answered 22/12, 2022 at 13:56 Comment(0)
E
0

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.

Educate answered 27/11, 2023 at 9:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.