long updateDnRepositoryContent(long fileId, byte[] content) throws Exception {
ResultSet rs = null;
String sql = "";
PreparedStatement statement = null;
try {
// sql =
// "UPDATE USR_DNREPOSITORY SET FILE_CONTENT=?,UPLOADDATE=SYSDATE,UPLOADUSER=? WHERE FILEID=?";
sql = "SELECT FILE_CONTENT FROM USR_DNREPOSITORY WHERE FILEID=? for update";
// ********** end
logger.info("updateDnRepositoryContent sql is: " + sql);
statement = connection.prepareStatement(sql);
statement.setLong(1, fileId);
rs = statement.executeQuery();
if (rs.next()) {
// **得到BLOB字段
Blob blob = rs.getBlob(1);
// 获取向blob写入二进制数据的输出流
// 1为起始字节位置
OutputStream os = blob.setBinaryStream(1);
os.write(content);
os.close();
}
} catch (Exception sqle) {
throw new SQLException(sqle.getMessage() + " - [ " + sql + " ]");
} finally {
if (rs != null)
rs.close();
if (statement != null)
statement.close();
}
return fileId;
}
也可以直接update bytes 数组,如:
public void updateFileById(String fileId, byte[] contentBytes) throws Exception {
String sql = "";
PreparedStatement statement = null;
try {
sql = "update USR_DNREPOSITORY set FILE_CONTENT = ? WHERE FILEID=?";
statement = connection.prepareStatement(sql);
statement.setBytes(1, contentBytes);
statement.setString(2, fileId);
statement.executeUpdate();
} catch (Exception sqle) {
throw new SQLException(sqle.getMessage() + " - [ " + sql + " ]");
} finally {
if (statement != null)
statement.close();
}
}