Cannot load driver class: oracle.jdbc.driver.OracleDriver

    描述:

           springboot 项目,由于不能直接从官网拉取  oracle 依赖,因此手动导入 oralce6.jar 包,程序正常执行,但是打包时,报如下错误。

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is java.lang.IllegalStateException: Cannot load driver class: oracle.jdbc.driver.OracleDriver
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
	at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:622) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
	... 97 common frames omitted
Caused by: java.lang.IllegalStateException: Cannot load driver class: oracle.jdbc.driver.OracleDriver
	at org.springframework.util.Assert.state(Assert.java:94) ~[spring-core-5.1.9.RELEASE.jar:5.1.9.RELEASE]
	at org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.determineDriverClassName(DataSourceProperties.java:222) ~[spring-boot-autoconfigure-2.1.8.RELEASE.jar:2.1.8.RELEASE]
	at org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.initializeDataSourceBuilder(DataSourceProperties.java:174) ~[spring-boot-autoconfigure-2.1.8.RELEASE.jar:2.1.8.RELEASE]
	at org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration.createDataSource(DataSourceConfiguration.java:43) ~[spring-boot-autoconfigure-2.1.8.RELEASE.jar:2.1.8.RELEASE]
	at org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration$Hikari.dataSource(DataSourceConfiguration.java:85) ~[spring-boot-autoconfigure-2.1.8.RELEASE.jar:2.1.8.RELEASE]
	at org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration$Hikari$$EnhancerBySpringCGLIB$$a3daaa7.CGLIB$dataSource$0() ~[spring-boot-autoconfigure-2.1.8.RELEASE.jar:2.1.8.RELEASE]
	at org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration$Hikari$$EnhancerBySpringCGLIB$$a3daaa7$$FastClassBySpringCGLIB$$7351cff7.invoke() ~[spring-boot-autoconfigure-2.1.8.RELEASE.jar:2.1.8.RELEASE]
	at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244) ~[spring-core-5.1.9.RELEASE.jar:5.1.9.RELEASE]
	at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:363) ~[spring-context-5.1.9.RELEASE.jar:5.1.9.RELEASE]
	at org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration$Hikari$$EnhancerBySpringCGLIB$$a3daaa7.dataSource() ~[spring-boot-autoconfigure-2.1.8.RELEASE.jar:2.1.8.RELEASE]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_201]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_201]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_201]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_201]
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
	... 98 common frames omitted

     解决办法:

            在 pom.xml 文件中加入如下依赖,通过 systemPath 指定 oracle6.jar 包所在的位置


    com.oracle
    ojdbc
    6
    system
    ${basedir}/lib/ojdbc6.jar 

          通过上述解决办法,在打包过程中不会报错了,但执行时,报同样的错误 !!!

 

         在此猜测,mvn install 不能将手动添加驱动打入 jar 包,在此只是猜测(由于还没有深入研究 maven),希望大神多多指点。

 

          解决办法二: 

                  定制库到Maven本地资源库,再通过 maven 依赖导入到项目中。

          1: 将 oralce6 驱动安装到本地

mvn install:install-file -DgroupId=com.oracle2 "-DartifactId=ojdbc62" "-Dversion=11.2.1" "-Dpackaging=jar" "-Dfile=./ojdbc6.jar"

注: DgroupId ,DartifactId,Dversion 这三个参数可以自己指定。

         Dfile 为 jar 包所在的路径

 

执行后如下图所示:

 

 

Cannot load driver class: oracle.jdbc.driver.OracleDriver_第1张图片

 

         2:  使用方法


    com.oracle2
    ojdbc62
    11.2.1

问题成功解决,亲测有效 。 .............................

 

 

你可能感兴趣的:(oracle,springboot,java,springboot,oracle)