Postgresql Groovy sql到字节

Postgresql Groovy sql到字节,postgresql,groovy,bytearray,Postgresql,Groovy,Bytearray,我必须得到一个存储在postgres数据库中的文档,并将其放入字节数组中 在Java中,这很好用 PreparedStatement ps = conn1.prepareStatement("SELECT document FROM documents WHERE documentname = ?"); ps.setString(1, "dingbatdocument.docx"); ResultSet rs = ps.executeQuery(); while (rs.next()) {

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

在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)
Groovy script throws an exception of type class org.postgresql.util.PSQLException with message = This ResultSet is closed.
对我来说,这意味着它试图证明它已经工作了,而不是给我和实际的字节数组

还有这个

def newSpelling = "dingbatdocument.docx";
byte[] val = sql.execute("SELECT document FROM documents WHERE documentname = ?", [newSpelling]);
并获取以下错误:

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)
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中获得与java中相同的结果,一个来自sql结果集的byte[]变量


提前感谢。

最后它非常简单,但不知道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是我需要的文档的字节[]表示