简体   繁体   中英

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

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

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

将文件Application.yml中的 db 驱动程序更新为

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

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 .

您可以检查SpringBoot 应用程序示例是否有帮助。

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
  • 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>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM