2011-12-26 29 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);