0

我有以下代码连接到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
4

1 回答 1

1

DataSet的 Groovy 源代码应该在类路径中。请参阅 API 中示例之后的摘录。

按照这个关于如何将源添加到类路径的问题。

样本:-

//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 Server 5.6.12。
  • mysql-connector-java-5.1.14手动下载,不使用@Grab
  • 在类路径中添加mysql-connector-java-5.1.14-bin.jar
  • 复制你的脚本(注意我没有static main(args))然后运行脚本。

详情

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]

控制台输出

第二种方法
我在 Groovy 控制台中测试了相同的方法,方法是直接从控制台将 jar 添加到类路径。得到了同样的结果。

第三种方法我使用如下所示
在 Groovy 控制台中测试了脚本。@Grab得到了同样的结果。

@GrabConfig(systemClassLoader=true)
@Grab('mysql:mysql-connector-java:5.1.14')

尝试遵循任何方法。

注意:-
我没有使用static main()它,因为 groovy 脚本不需要它,尽管我也成功地使用它进行了测试。

如果您仍然遇到任何问题,请尝试使用此处提到的 root 加载程序加载 jar 。

于 2013-06-12T01:25:18.600 回答