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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/54305348/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 01:01:03  来源:igfitidea点击:

How to connect to oracle database using spring boot

javaoraclespring-bootdriver

提问by Dhanushka Sampath

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.

我正在使用 Spring Boot 应用程序并尝试访问 Oracle 数据库。尽管它已成功构建,但当我尝试在 Kubernetes 中部署时,它给出了以下错误。

I changed the application.propertiesfile and pom.xmlfile with below configurations:

我使用以下配置更改了application.properties文件和pom.xml文件:

Application.yml

应用程序.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

POM文件

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

回答by Darshan Mehta

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

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

Driver can be downloaded from here.

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

回答by smilyface

Maven dependency:

Maven 依赖:

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

application.yml file :

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

Note: driver.class-name

注意driver.class-name

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

有时您可能需要添加spring.jpa.database-platform=org.hibernate.dialect.Oracle10gDialect到 application.yml 文件(对于 Oracle 10)。

回答by Nirmala

You can check if the SpringBoot appexample helps.

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

回答by abdullah imran

Add below dependency and repository in the pom

在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

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

回答by user12264329

Update the db driver in the file Application.ymlto

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

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