如何在加载groovy类时导入文件?

时间:2012-04-23 12:06:16

标签: groovy

我正在加载一个类并执行一些sql select语句,似乎需要导入import groovy.sql.Sql。如何在加载类时加载一些库?

我的代码(如果删除Sql操作,则工作正常)

def fClass = new GroovyClassLoader().parseClass( new File( 'plugin/Pi.groovy' ) )
result=fClass.newInstance().buildTags( params, i9piparams, "userRoleCount")

pi.groovy

public class Pi{
    def result

    private def getDbUrl(dbdriver,dbhost,dbport,dbname)
    {
        return "$dbdriver:@$dbhost:$dbport:$dbname"
    }

    public def buildTags(Map<String,String> params,Map<String,String> i9piparams,def i9piType)
    {

        println params
        println i9piparams

        /*some Sql operation*/
        Driver="oracle.jdbc.driver.OracleDriver"
            dbdriver="jdbc:oracle:thin"
        def url=getDbUrl(dbdriver,params.tns,i9piparams.dbport,i9piparams.dbname)           

            def sql = Sql.newInstance(url,params.u,params.x,Driver)

            sql.eachRow("select name, value from v\$parameter where name = 'open_cursors'"){ row ->
                result.name=row.name
                result.value=row.value
            }

    }
}

输出

[pd:admin, u:zx5531d, tns:foner, dh:abds, dn:D35531, dp:11531, un:admin, x:cx15531]
[:, dbname:orcl, dbport:1521, dbtype:oracle]
Exception in thread "main" groovy.lang.GroovyRuntimeException: Could not find matching constructor for: groovy.sql.Sql(org.codehaus.groovy.runtime.GStringImpl, groovy.util.slurpersupport.Attributes, java.lang.String, java.lang.String)
.
.
.

1 个答案:

答案 0 :(得分:2)

您可以尝试更改您的专栏:

def sql = Sql.newInstance(url,params.u,params.x,Driver)

def sql = Sql.newInstance( url, 'zx5531d', params.x, Driver )

我怀疑当您从XML中读取属性时需要对属性调用text(),以便获得String而不是groovy.util.slurpersupport.Attributes类。

另外,为什么Driver获得了首字母大写字母?

并且(不确定parameter是什么),但您可能想要改变:

"select name, value from v\$parameter where name = 'open_cursors'"

"select name, value from v\${Sql.expand(parameter)} where name = 'open_cursors'"