使用系统类加载器与葡萄时出错

时间:2016-05-31 20:59:02

标签: java groovy

我正在用Java编写一个简单的Groovy程序,该程序从数据库中读取内容:

@Grab('com.oracle:ojdbc6:11.2.0.1.0')
@Grab(group='org.codehaus.groovy', module='groovy-sql', version='2.4.6')

import groovy.sql.Sql

def db = [url: "jdbc:oracle:thin:@localhost:1521:XE",
          user: 'user_kshitiz', password: 'a', driver: 'oracle.jdbc.driver.OracleDriver']
def sql = Sql.newInstance(db.url, db.user, db.password, db.driver)

然而,这给了我一个java.sql.SQLException: No suitable driver found错误。 所以我将葡萄配置为使用系统类加载器:

@Grapes([
        @Grab('com.oracle:ojdbc6:11.2.0.1.0'),
        @GrabConfig(systemClassLoader = true)
])

这也会导致以下错误:

Information:Groovyc: java.lang.NoSuchMethodException: org.codehaus.groovy.runtime.dgm$518.<init>(java.lang.String, org.codehaus.groovy.reflection.CachedClass, java.lang.Class, [Ljava.lang.Class;)
    at java.lang.Class.getConstructor0(Class.java:2849)
    at java.lang.Class.getConstructor(Class.java:1718)
    at org.codehaus.groovy.reflection.GeneratedMetaMethod$Proxy.createProxy(GeneratedMetaMethod.java:92)
    at org.codehaus.groovy.reflection.GeneratedMetaMethod$Proxy.proxy(GeneratedMetaMethod.java:83)
    at org.codehaus.groovy.reflection.GeneratedMetaMethod$Proxy.isValidMethod(GeneratedMetaMethod.java:68)
    at groovy.lang.MetaClassImpl.chooseMethodInternal(MetaClassImpl.java:3172)
    at groovy.lang.MetaClassImpl.chooseMethod(MetaClassImpl.java:3134)
    at groovy.lang.MetaClassImpl.getMethodWithCachingInternal(MetaClassImpl.java:1330)
    at groovy.lang.MetaClassImpl.createPojoCallSite(MetaClassImpl.java:3367)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.createPojoSite(CallSiteArray.java:132)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.createCallSite(CallSiteArray.java:166)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
    at groovy.grape.GrapeIvy.grab(GrapeIvy.groovy:271)
    at groovy.grape.Grape.grab(Grape.java:167)
    at groovy.grape.GrabAnnotationTransformation.visit(GrabAnnotationTransformation.java:378)
    at org.codehaus.groovy.transform.ASTTransformationVisitor$3.call(ASTTransformationVisitor.java:321)
    at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:931)
    at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:593)
    at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:569)
    at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:546)
    at org.jetbrains.groovy.compiler.rt.GroovyCompilerWrapper.compile(GroovyCompilerWrapper.java:58)
    at org.jetbrains.groovy.compiler.rt.DependentGroovycRunner.runGroovyc(DependentGroovycRunner.java:116)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.jetbrains.groovy.compiler.rt.GroovycRunner.intMain2(GroovycRunner.java:132)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.jetbrains.jps.incremental.groovy.InProcessGroovyc.runGroovycInThisProcess(InProcessGroovyc.java:156)
    at org.jetbrains.jps.incremental.groovy.InProcessGroovyc.access$000(InProcessGroovyc.java:51)
    at org.jetbrains.jps.incremental.groovy.InProcessGroovyc$1.call(InProcessGroovyc.java:85)
    at org.jetbrains.jps.incremental.groovy.InProcessGroovyc$1.call(InProcessGroovyc.java:82)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)

如何在Groovy中创建数据库连接?

2 个答案:

答案 0 :(得分:0)

AFAIK,Oracle驱动程序在Maven Central中无法公开访问。你有一些选择:

  1. 访问包含Oracle JDBC驱动程序的一些非公共Maven存储库

  2. 从Oracle站点手动下载Oracle JDBC驱动程序,将其导入本地Maven存储库或类似工件的服务,并将此存储库包含在构建脚本中

答案 1 :(得分:0)

Oracle Maven存储库上提供了JDBC驱动程序。有关详细信息,请按照此blog中的说明进行操作。