2015-02-24 78 views
0

我必须得到一个存储在postgres数据库中的文档,并将其放入一个字节数组中。Groovy sql to byte

在Java中,这只是正常

PreparedStatement ps = conn1.prepareStatement("SELECT document FROM documents WHERE documentname = ?"); 
ps.setString(1, "dingbatdocument.docx"); 
ResultSet rs = ps.executeQuery(); 
while (rs.next()) { 
    byte[] documentBytes = rs.getBytes(1); 
} 

,但我不得不使用Groovy此代码一窍不通如何做到这一点,到目前为止,我已经试过这

def newSpelling = "dingbatdocument.docx"; 
def val = sql.execute("SELECT document FROM documents WHERE documentname = ?", [newSpelling]) as byte[]; 

和得到这个错误:

Caused by: org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'true' with class 'java.lang.Boolean' to class 'byte' 
at korg.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToNumber(DefaultTypeTransformation.java:146) 

这对我来说,它试图资产,它已经工作,而不是给我一个第二实际字节数组,

def newSpelling = "dingbatdocument.docx"; 
byte[] val = sql.execute("SELECT document FROM documents WHERE documentname = ?", [newSpelling]); 

,并得到这个错误:

Groovy script throws an exception of type class org.postgresql.util.PSQLException with message = This ResultSet is closed. 

最后这一点:

def reqdColName = "document"; 
    def reqdDocument = "dingbatdocument.docx"; 
    def query1 = "SELECT $reqdColName FROM documents WHERE documentname = '$reqdDocument'"; 
    def documentBytes = conn1.executeQuery(query1).getArray(reqdColName); 

这也给了我

Groovy script throws an exception of type class org.postgresql.util.PSQLException with message = This ResultSet is closed. 

所以我的问题是如何在groovy中得到与我在sql结果集中的java [byte]变量相同的结果?

在此先感谢。

回答

0

最后它很容易,但不知道Groovy是不同的。这里是我如何做到底,

def reqdColName = "document"; 
    def reqdDocument = "documentName.docx"; 
    def query1 = "SELECT * FROM documents WHERE documentname = '$reqdDocument'"; 


    byte[] myData; 
    conn1.eachRow(query1){ 
     row ->  
     if(debug){logger.severe(dI+thisTrace+"myData \n" 
     +"\t"+row.documentid+"\n" 
     +"\t"+row.documentname 
     +"\t"+row.versionmajor +"\n" 
     +"\t"+row.versionminor +"\n" 
     +"\t"+row.versionmini +"\n" 
     +"\t"+row.uploader +"\n" 
     +"\t"+row.approver +"\n" 
     +"\t"+row.uploaddate +"\n" 
     +"\t"+row.approvaldate +"\n" 
//  +"\t"+row.document+"\n" 
     );} 
     myData = row.document 
    } 

myData是我需要的文档的byte []表示。