groovy.lang.MissingPropertyException: No such property: Sql for class异常
程序员文章站
2024-03-17 12:29:46
...
代码
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!"; }