我有以下代码连接到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
答案 0 :(得分:1)
DataSet的Groovy源应该在类路径上。请参阅API中示例后的摘录。
关注如何将源添加到类路径的this question。
示例: -
//Add ojdbc14.jar to groovy classpath for the script
>groovy -cp C:\Oracle\11gR2\jdbc\lib\ojdbc14 DataSetTest.groovy
//DataSetTest.groovy
import groovy.sql.Sql
def db = [url:'jdbc:oracle:thin:@myoradb:1521:myoradb', user:'johndoe',
password:'anything', driver:'oracle.jdbc.driver.OracleDriver']
def sql = Sql.newInstance(db.url, db.user, db.password, db.driver)
def airport = sql.dataSet("AIRPORT")
def cmh = airport.findAll{it.airportid == "KCMH"}
println cmh.sql
//Prints
select * from AIRPORT where airportid = ?
更新 [Groovy 2.1.4]
第一种方法
我通过将源放在类路径中测试了您使用的相同样本,它对我来说就像一个魅力。以下步骤:
mysql-connector-java-5.1.14
@Grab
mysql-connector-java-5.1.14-bin.jar
。static main(args)
)&然后运行脚本。Detials
import groovy.sql.Sql
def dbUrl = 'jdbc:mysql://localhost:3306/stackoverflowdb'
def dbUser = 'root'
def dbPassword = ''
def driverClass = 'com.mysql.jdbc.Driver'
def sql = Sql.newInstance(dbUrl, dbUser, dbPassword, driverClass)
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 "First Row First Name: $results"
//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}
命令提示符:
//Command:-
groovy -cp C:\tools\MySql\connector\mysql-connector-java-5.1.14\mysql-connector-java-5.1.14-bin.jar DataSetTest.groovy
结果:
First Row First Name: James
[id:3, firstname:Sam, lastname:Pullara, location_id:30, location_name:California]
Sort on First Name
[id:2, firstname:Bob, lastname:Mcwhirter, location_id:20, location_name:Atlanta]
[id:1, firstname:James, lastname:Strachan, location_id:10, location_name:London]
[id:3, firstname:Sam, lastname:Pullara, location_id:30, location_name:California]
第二种方法
我通过直接从控制台向jarpath添加jar,在Groovy Console中测试了相同的内容。得到了相同的结果。
第三种方法
我使用@Grab
在Groovy Console中测试了脚本,如下所示。得到了相同的结果。
@GrabConfig(systemClassLoader=true)
@Grab('mysql:mysql-connector-java:5.1.14')
尝试遵循任何方法。
注: -
我没有使用static main()
因为groovy脚本不需要它,尽管我也用它成功测试过。
如果您仍然遇到任何问题,请尝试使用根加载程序加载jar,如提及here。