mysqlに接続する次のコードがあります。エラー メッセージを表示するデータセットの 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