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

2351 ワード


コード#コード#
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!";
}