groovy.lang.MissingPropertyException: No such property: Sql for class异常

 

代码

package groovy

/**
 * 样例组件4
 */
def void sample4(){
    def sql = Sql.newInstance("jdbc:mysql://127.0.0.1:3306/test", "root",
            "123456", "com.mysql.jdbc.Driver");
    def aa = "";
    sql.eachRow("select * from temp") {
        println it.aa +  " ${it.bb}";
        aa = it.aa;
    }
    println "Hello Groovy!";
}

 

 

异常

 

Exception in thread "main" groovy.lang.MissingPropertyException: No such property: Sql for class: groovy.test
	at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:50)
	at org.codehaus.groovy.runtime.callsite.PogoGetPropertySite.getProperty(PogoGetPropertySite.java:49)
	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(AbstractCallSite.java:231)
	at groovy.test.sample4(test.groovy:8)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
	at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
	at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1047)
	at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:877)
	at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:690)
	at groovy.lang.GroovyObjectSupport.invokeMethod(GroovyObjectSupport.java:44)
	at groovy.lang.Script.invokeMethod(Script.java:78)
	at Runner.runWithGroovyShell(Runner.java:28)
	at Runner.main(Runner.java:70)

 

 

这个异常的原因是没有import Sql包,添加“import groovy.sql.Sql;”就行。新代码如下:

 

package groovy
import groovy.sql.Sql; // 加入这段代码

/**
 * 样例组件4
 */
def void sample4(){
    def sql = Sql.newInstance("jdbc:mysql://127.0.0.1:3306/test", "root",
            "123456", "com.mysql.jdbc.Driver");
    def aa = "";
    sql.eachRow("select * from temp") {
        println it.aa +  " ${it.bb}";
        aa = it.aa;
    }
    println "Hello Groovy!";
}

 

 

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值