Spring boot + oracle 驱动 + 没有合适的驱动

Posted

技术标签:

【中文标题】Spring boot + oracle 驱动 + 没有合适的驱动【英文标题】:Spring boot + oracle driver + No suitable driver 【发布时间】:2020-12-03 18:01:34 【问题描述】:

我的应用程序使用许多数据源(postgreSQL、Sybase) 我尝试添加一个 Oracle 数据源

Application.properties

rocbis.datasource.prd.driver.class-name=oracle.jdbc.driver.OracleDriver
rocbis.datasource.prd.jdbc-url=jdbc:oracle:thin:@<host>:1521:<dbname>
rocbis.datasource.prd.username=<username>
rocbis.datasource.prd.password=<password>

主机、数据库名、用户名和密码已被混淆...

配置类

@Bean(name = "rocbisDatasourcePRD")
@ConfigurationProperties(prefix = "rocbis.datasource.prd")
public DataSource rocbisDataSourcePRD() 
    return DataSourceBuilder.create().build();


@Bean(name = "rocbisJDBCTemplatePRD")
public JdbcTemplate rocbisJdbcTemplatePRD(@Qualifier("rocbisDatasourcePRD") DataSource ds) 
    return new JdbcTemplate(ds);

和 POM.xml

<dependency>
    <groupId>com.oracle.database.jdbc</groupId>
    <artifactId>ojdbc8</artifactId>
    <version>19.6.0.0</version>
</dependency>
<dependency>
    <groupId>com.oracle.database.jdbc</groupId>
    <artifactId>ucp</artifactId>
    <version>19.6.0.0</version>
</dependency>

当 Springboot 启动时出现以下错误:

Caused by: java.lang.RuntimeException: Failed to get driver instance
for jdbcUrl=jdbc:oracle:thin:@<host>:1521:<dbname> at com.zaxxer.hikari.util.DriverDataSource.<init>DriverDataSource.java:112)
at com.zaxxer.hikari.pool.PoolBase.initializeDataSource(PoolBase.java:336)
at com.zaxxer.hikari.pool.PoolBase.<init>(PoolBase.java:109)    
at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:108)    
at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112)
at config.DatasourceConfiguration.rocbisJdbcTemplatePRD(DatasourceConfiguration.java:446)
at config.DatasourceConfiguration$$EnhancerBySpringCGLIB$$1cdc8e91.CGLIB$rocbisJdbcTemplatePRD$59(<generated>)
at config.DatasourceConfiguration$$EnhancerBySpringCGLIB$$1cdc8e91$$FastClassBySpringCGLIB$$9002c681.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244)
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:363)
at config.DatasourceConfiguration$$EnhancerBySpringCGLIB$$1cdc8e91.rocbisJdbcTemplatePRD(<generated>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)  
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)  
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)  
at java.lang.reflect.Method.invoke(Unknown Source)  
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
        ... 64 common frames omitted 
Caused by: java.sql.SQLException: No suitable driver    
at java.sql.DriverManager.getDriver(Unknown Source)
at com.zaxxer.hikari.util.DriverDataSource.<init>(DriverDataSource.java:104)
            ... 79 common frames omitted
             - org.springframework.boot.SpringApplication

我试过了:

driver-class-name 而不是 driver.class-name

oracle.jdbc.driver.OracleDriver 代替 oracle.jdbc.OracleDriver

有什么想法吗? 谢谢

【问题讨论】:

【参考方案1】:

使用oracle.jdbc.OracleDriver 而不是oracle.jdbc.driver.OracleDriver


以下配置适用于我的 Oracle

pom.xml

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

bootstrap.properties

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

【讨论】:

以上是关于Spring boot + oracle 驱动 + 没有合适的驱动的主要内容,如果未能解决你的问题,请参考以下文章

运行 Spring Boot 简单应用程序时找不到 Oracle 驱动程序

spring boot整合mybitas怎么配置oracle?

Spring Boot中使用MyBatis

spring-boot-oracle spring-batch

Spring Boot 从 Oracle 配置入手

带有 Cassandra 3.x 驱动程序的 Spring Boot