java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > SpringBoot使用Oracle11g

如何在SpringBoot项目中使用Oracle11g数据库

作者:皮卡卡西

这篇文章主要介绍了在SpringBoot项目中使用Oracle11g数据库的操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

在SpringBoot项目中使用Oracle11g数据库

具体步骤如下:

1:下载ojdbc6.jar ,随便放个英文目录位置就好

2:命令行下输入下行,注意最后-Dfile 为自己的下载目录

mvn install:install-file -DgroupId=com.oracle -DartifactId=ojdbc6 -Dversion=11.2.0.1.0 -Dpackaging=jar -Dfile=H:\eclpise-workspace\ojdbc6.jar

3:在项目的pom文件中引入下面依赖

<dependency>
     <groupId>com.oracle</groupId>
     <artifactId>ojdbc6</artifactId>
     <version>11.2.0.1.0</version>
 </dependency>

4:在项目的配置文件中写数据库的连接信息,其中,假设Oracle连接ip为127.0.0.1,端口号为1521,数据库为database,用户名为username,密码为password

spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@127.0.0.1:1521:database
spring.datasource.username=username
spring.datasource.password=password

完成!

SpringBoot 2.0连接Oracle 11g遇到的问题

基本配置信息:

驱动 oracle.jdbc.driver.OracleDriver
连接Url jdbc:oracle:thin:@192.168.112.129:1521:orcl
用户 System
密码 Aa123456

添加驱动包:

这里添加的是ojdbc6包,因为之前在Davinci系统使用ojdbc14时报错了,我使用的Jdk1.8的版本。

ojdbc6.jar比ojdbc14.jar的版本高得多,因为里面的6是指支持jdk1.6 ,而14是指支持jdk1.4。

        <dependency>
            <groupId>com.oracle.database.jdbc</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>11.2.0.4</version>
        </dependency>

创建IDatabasePlatform时出现报错

<!--org.jumpmind.db.platform.JdbcDatabasePlatformFactory-->
Constructor<? extends IDatabasePlatform> construtor = clazz.getConstructor(DataSource.class, SqlTemplateSettings.class);
            IDatabasePlatform platform = construtor.newInstance(dataSource, settings);
 
2020-10-13 16:57:47.929  INFO 13740 --- [manager-startup] o.j.metl.core.runtime.AgentRuntime       : Deploying the 本地数据库 resource to the <default user on DESKTOP-INGH9R2> agent
resourceId:abc9cfbe-44cf-4e83-9eb1-325422db9dd8
sql:select AIRDATA,AIRTIME from MAI_DATA
projectVersionId:f55241fd-9564-4f6a-bd0d-95a50a729000
2020-10-13 16:57:49.150  INFO 13740 --- [manager-startup] o.j.metl.core.runtime.AgentRuntime       : Deploying the Oracle数据源 resource to the <default user on DESKTOP-INGH9R2> agent
2020-10-13 16:57:49.308  INFO 13740 --- [nio-9999-exec-1] o.j.d.p.JdbcDatabasePlatformFactory      : Detected database 'Oracle', version '11', protocol 'oracle'
2020-10-13 16:57:49,342 ERROR [GlobalExceptionHandler.java:47] : requestUrl=http://localhost:9999/open/api/ml/comp/handler?name=rdbmsreader&param=%7B%22sql%22:%22select+AIRDATA,AIRTIME+from+MAI_DATA%22,%22resourceId%22:%22abc9cfbe-44cf-4e83-9eb1-325422db9dd8%22,%22projectVersionId%22:%22f55241fd-9564-4f6a-bd0d-95a50a729000%22%7D  
2020-10-13 16:57:49,343 ERROR [GlobalExceptionHandler.java:27] : System_error:requestId=1516430392  
org.jumpmind.db.platform.DdlException: Could not create a platform of type Oracle
	at org.jumpmind.db.platform.JdbcDatabasePlatformFactory.createNewPlatformInstance(JdbcDatabasePlatformFactory.java:166)
	at org.jumpmind.metl.ui.views.design.RdbmsHandler.sqlExecute(RdbmsHandler.java:68)
	at org.jumpmind.metl.ui.views.design.RdbmsHandler.execute(RdbmsHandler.java:42)
	at org.jumpmind.metl.core.handler.HandlerService.execute(HandlerService.java:14)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:338)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:197)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
	at org.springframework.aop.support.DelegatingIntroductionInterceptor.doProceed(DelegatingIntroductionInterceptor.java:136)
	at org.springframework.aop.support.DelegatingIntroductionInterceptor.invoke(DelegatingIntroductionInterceptor.java:124)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
	at com.sun.proxy.$Proxy138.execute(Unknown Source)
	at com.yak.api.mlengine.CompController.handler(CompController.java:53)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:209)
	at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)
	at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:870)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:776)
	at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:991)
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925)
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:978)
	at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:870)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
	at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:855)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.filterAndRecordMetrics(WebMvcMetricsFilter.java:158)
	at org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.filterAndRecordMetrics(WebMvcMetricsFilter.java:126)
	at org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.doFilterInternal(WebMvcMetricsFilter.java:111)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at com.yak.platform.api.config.CORSFilter.doFilter(CORSFilter.java:71)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.springframework.boot.actuate.web.trace.servlet.HttpTraceFilter.doFilterInternal(HttpTraceFilter.java:84)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:496)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
	at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803)
	at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
	at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:790)
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1459)
	at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.reflect.InvocationTargetException
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
	at org.jumpmind.db.platform.JdbcDatabasePlatformFactory.createNewPlatformInstance(JdbcDatabasePlatformFactory.java:160)
	... 71 more
Caused by: java.lang.NoClassDefFoundError: org/springframework/jdbc/support/lob/OracleLobHandler
	at org.jumpmind.db.platform.oracle.OracleLobHandler.<init>(OracleLobHandler.java:16)
	at org.jumpmind.db.platform.oracle.OracleDatabasePlatform.createSqlTemplate(OracleDatabasePlatform.java:70)
	at org.jumpmind.db.platform.oracle.OracleDatabasePlatform.createSqlTemplate(OracleDatabasePlatform.java:34)
	at org.jumpmind.db.platform.AbstractJdbcDatabasePlatform.<init>(AbstractJdbcDatabasePlatform.java:47)
	at org.jumpmind.db.platform.oracle.OracleDatabasePlatform.<init>(OracleDatabasePlatform.java:55)
	... 76 more
Caused by: java.lang.ClassNotFoundException: org.springframework.jdbc.support.lob.OracleLobHandler
	at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
	... 81 more

解决方案:

经查询,在初始化OracleJdbcSqlTemplate时,由于SpringBoot2.0中使用的是Spring-jdbc-5.0.4,出现了上面的报错信息。

该代码之前使用spring4的spring-jdbc.jar就不会报错,因为spring 5之后就不存在OracleLobHandler这个类了。

由于不想修改代码,继续沿用老版本:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>4.2.6.RELEASE</version>
  </dependency>

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文