我有以下代码连接到mysql。dataset findAll 方法给出错误消息。
代码:
def people = sql.dataSet("PERSON")
people.add( firstname:"James", lastname:"Strachan", id:1, location_id:10, location_name:'London' )
people.add( firstname:"Bob", lastname:"Mcwhirter", id:2, location_id:20, location_name:'Atlanta' )
people.add( firstname:"Sam", lastname:"Pullara", id:3, location_id:30, location_name:'California' )
def janFrequentBuyers = people.findAll { it.location_id > 10 && it.lastname == "Pullara" }.sql
println janFrequentBuyers
错误信息:
Caught: groovy.lang.GroovyRuntimeException: DataSet unable to evaluate expression. AST not available for closure: testdb_closure7. Is the source code on the classpath?
请帮我解决这个错误。
这是我使用 MySQL 的示例代码:
static main(args) {
def sql = Sql.newInstance(url, username, password, driverClassName)
try {
sql.execute("drop table PERSON")
} catch (Exception e) {}
// create table
sql.execute('''create table PERSON (
id integer not null primary key,
firstname varchar(20),
lastname varchar(20),
location_id integer,
location_name varchar(30)
)''')
// now let's populate the table
def people = sql.dataSet("PERSON")
people.add(firstname: "James", lastname: "Strachan", id: 1, location_id: 10, location_name: 'London')
people.add(firstname: "Bob", lastname: "Mcwhirter", id: 2, location_id: 20, location_name: 'Atlanta')
people.add(firstname: "Sam", lastname: "Pullara", id: 3, location_id: 30, location_name: 'California')
//get first now
def results = sql.firstRow("select firstname, lastname from PERSON where id=1").firstname
println results
//query using where class
def janFrequentBuyers = people.findAll { it.location_id > 10 && it.lastname == "Pullara" }
println janFrequentBuyers.each { println it }
}
错误信息:
groovy -cp ./mysql-connector-java-5.1.14-bin.jar j.gy
James
Caught: groovy.lang.GroovyRuntimeException: DataSet unable to evaluate expression. AST not available for closure: testdb$_main_closure1. Is the source code on the classpath?
groovy.lang.GroovyRuntimeException: DataSet unable to evaluate expression. AST not available for closure: testdb$_main_closure1. Is the source code on the classpath?
at testdb.main(j.gy:36)
我将其更改为 h2 DB,仍然出现相同的错误:
groovy db.gy
Caught: groovy.lang.GroovyRuntimeException: DataSet unable to evaluate expression. AST not available for closure: db$_run_closure1. Is the source code on the classpath?
groovy.lang.GroovyRuntimeException: DataSet unable to evaluate expression. AST not available for closure: db$_run_closure1. Is the source code on the classpath?
at db.run(db.gy:19)
代码:
@GrabConfig(systemClassLoader=true)
@Grab(group='com.h2database', module='h2', version='1.3.168')
import groovy.sql.Sql
def sql = Sql.newInstance("jdbc:h2:mem:db1", "sa", "sa", "org.h2.Driver")
sql.execute('''create table PERSON (
id integer not null primary key,
firstname varchar(20),
lastname varchar(20),
location_id integer,
location_name varchar(30)
)''')
// now let's populate the table
def people = sql.dataSet("PERSON")
people.add(firstname: "James", lastname: "Strachan", id: 1, location_id: 10, location_name: 'London')
people.add(firstname: "Bob", lastname: "Mcwhirter", id: 2, location_id: 20, location_name: 'Atlanta')
people.add(firstname: "Sam", lastname: "Pullara", id: 3, location_id: 30, location_name: 'California')
//query using where class
def janFrequentBuyers = people.findAll { it.location_id > 10 && it.lastname == "Pullara"}
janFrequentBuyers.each{println it}
println "Sort on First Name"
people.sort{it.firstName}.each{println it}
忘了和你核对一下你使用的是什么版本的 groovy?
groovy -version
Groovy Version: 2.1.2 JVM: 1.6.0_24 Vendor: Sun Microsystems Inc. OS: Linux