如何使用spring boot连接oracle数据库

问题描述 投票:0回答:9

我正在使用 Spring Boot 应用程序并尝试访问 Oracle 数据库。虽然构建成功,但是当我尝试在 Kubernetes 中部署时却出现以下错误。

我使用以下配置更改了

application.properties
文件和
pom.xml
文件:

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 文件

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

异常

***************************
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   
java oracle spring-boot driver
9个回答
8
投票

Maven 依赖:

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

application.yml 文件:

# 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

注意driver.class-name

有时您可能需要将

spring.jpa.database-platform=org.hibernate.dialect.Oracle10gDialect
添加到 application.yml 文件(适用于 Oracle 10)。


4
投票

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

还要在 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

1
投票

将文件中的数据库驱动程序更新为

Application.yml

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

0
投票

您需要下载 Oracle JDBC 驱动程序 jar 文件并将其添加到您的类路径中,以便您的应用程序加载

oracle.jdbc.OracleDriver
类。

驱动程序可以从这里下载。


0
投票

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


0
投票

对于 Oracle 数据库,

Maven 设置:

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

属性设置:

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

请勿使用

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

0
投票
  • 您只需将这两个依赖项添加到您的

    pom.xml
    文件中即可正常工作。

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

0
投票

在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 

注意:

  1. org.hibernate.dialect.Oracle10gDialect 已被弃用。
  2. 您需要检查 { jdbc:oracle:thin:@localhost:1521:ORCL} 数据库 URL 的最后部分是否为 {ORCL} 或 {XE}。请参阅随附的屏幕截图。 enter image description here

0
投票

我使用了

implementation 'com.oracle:ojdbc7:12.1.0.2.0
gradle 依赖项。

在 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

如果它不适合您,您可以使用不同的

OracleDialect

对我来说,上面的代码运行没有任何问题。

© www.soinside.com 2019 - 2024. All rights reserved.