2011-12-26 63 views
0

我有一个数据库表,其中一列(declatationform)的类型是“BYTEA”我要存储在此列图像[]所以我必须返回一个方法是准备语句字节在Java中

public void uploadRentProofDeclaration(final MultipartFile declarationForm, 
     final int rentProofInfoId, int year) { 

    String updateSql = "update plit_landlordinfo" + year 
      + " set filename=?,declarationform=? where cid=?"; 
    getJdbcTemplate().execute(updateSql, new PreparedStatementCallback() { 
     public Object doInPreparedStatement(
       final PreparedStatement pSstatement) throws SQLException, 
       DataAccessException { 
      pSstatement.setString(1, declarationForm.getOriginalFilename()); 

      try { 
       pSstatement.setBinaryStream(2, new ByteArrayInputStream(
         declarationForm.getBytes()), declarationForm 
         .getBytes().length); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      pSstatement.setInt(3, rentProofInfoId); 

      pSstatement.execute(); 
      return null; 
     } 
    }); 
} 

这里正在发生的事情是文件名是越来越节省,但字节[]不保存仍然是列在我的表空白,它是不会放弃任何错误任何一个可以帮助我什么问题

回答

0

注知道如何getBytes()declarationForm作品(即方法被称为两次)?尝试从getBytes()方法中存储字节数组。

byte []bytes=declarationForm.getBytes(); 
pSstatement.setBinaryStream(2, new ByteArrayInputStream(bytes),bytes.length); 

或者您也可以使用PreparedStatement.setBytes()方法。

byte []bytes=declarationForm.getBytes(); 
pSstatement.setBytes(2,bytes);