2012-04-23 49 views
1

我正在加載一個類並執行一些sql select語句,似乎需要導入import groovy.sql.Sql。如何在加載課程時加載一些圖書館?如何在加載groovy類時導入文件?

我的代碼(如果我刪除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) 
. 
. 
. 

回答

2

你可以嘗試改變你的線路:

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

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

我懷疑的是,你需要調用text()上的屬性,當你從XML閱讀它們,讓你得到一個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'" 
+0

您好,我作出相應的chages和它給我'無效Url'這裏我的網址'jdbc:oracle:thin:@foner:1521:orcl'。另外關於Driver,我正在重構一箇舊代碼,所以錯過了它。 – abi1964 2012-04-23 12:30:19

+0

你在系統類路徑上是否有oracle驅動程序? – 2012-04-23 12:32:36

+1

@AbhishekSimon PS:我更新了我的[上一個答案](http://stackoverflow.com/a/10278678/6509)將'text()'調用應用於每個屬性調用 – 2012-04-23 12:33:41