日期:2014-05-16 浏览次数:20532 次
@Transactional public void storeImage(final String name, final InputStream contentStream, final int contentLength, final String description) throws DataAccessException { if (lobHandler instanceof OracleLobHandler) { try { PreparedStatement ptst = getConnection() .prepareStatement( "INSERT INTO IMAGEDB VALUES(? , EMPTY_BLOB() , EMPTY_CLOB())"); ptst.setString(1, name); ptst.executeUpdate(); // Execute SQL statement ptst = getConnection() .prepareStatement( "SELECT CONTENT ,DESCRIPTION FROM IMAGEDB WHERE IMAGE_NAME = ? FOR UPDATE"); ptst.setString(1, name); ResultSet rs = ptst.executeQuery(); rs.next(); Blob contentBlob = rs.getBlob(1); OutputStream blobOutputStream = ((BLOB) contentBlob) .getBinaryOutputStream(); FileCopyUtils.copy(contentStream, blobOutputStream); Clob descClob = rs.getClob(2); Writer clobWriter = ((oracle.sql.CLOB) descClob) .getCharacterOutputStream(); clobWriter.write(description); clobWriter.close(); contentStream.close(); blobOutputStream.close(); getConnection().commit(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else { getJdbcTemplate() .execute( "INSERT INTO imagedb (image_name, content, description) VALUES (?, ?, ?)", new AbstractLobCreatingPreparedStatementCallback( this.lobHandler) { protected void setValues(PreparedStatement ps, LobCreator lobCreator) throws SQLException { ps.setString(1, name); lobCreator.setBlobAsBinaryStream(ps, 2, contentStream, contentLength); lobCreator.setClobAsString(ps, 3, description); } }); } }