81,092
社区成员
发帖
与我相关
我的任务
分享
/**
* oracle中導入模板
*
* @param tableName
* @param filedName
* @param id
* @param t
* @throws SQLException
*/
public static int blobUpdateForOracle(DataSource ds, String tableName, String filedName, String whereStr, OutputStreamWriter outputWriter)
throws SQLException {
if (logger.isDebugEnabled()) {
logger.debug("blobUpdateForOracle(String, String, String, String, byte[]) - start"); //$NON-NLS-1$
}
String selectSql = "select " + filedName + " from " + tableName + " where " + whereStr + " for update";
String updateSql = "UPDATE " + tableName + " SET " + filedName + "=EMPTY_BLOB() WHERE " + whereStr;
Statement stmt = null;
ResultSet rs = null;
int result = -1;
Connection cn = null;
try {
cn = ds.getConnection();
boolean defaultCommit = cn.getAutoCommit();
cn.setAutoCommit(false);
stmt = cn.createStatement();
result = stmt.executeUpdate(updateSql);
rs = stmt.executeQuery(selectSql);
while (rs.next()) {
BufferedOutputStream out = null;
try {
Blob blob = rs.getBlob(1);
out = new BufferedOutputStream(blob.setBinaryStream(1));
outputWriter.write(out);
try {
out.close();
} catch (Exception e) {
logger.warn("blobUpdateForOracle:out.close", e);
}
cn.commit();
} catch (Exception e) {
cn.rollback();
logger.warn("blobUpdateForOracle:", e);
throw new SQLException("更新失败:" + e.getMessage());
} finally {
if (out != null) {
try {
out.close();
} catch (Exception e) {
// logger.warn("blobUpdateForOracle:out.close", e);
}
}
}
}
cn.setAutoCommit(defaultCommit);
} catch (Exception e) {
logger.warn("blobUpdateForOracle:" + whereStr, e);
throw new SQLException("更新失败:" + e.getMessage());
} finally {
if (rs != null) {
try {
rs.close();
} catch (Exception e) {
logger.error("blobUpdateForOracle", e);
}
}
if (stmt != null) {
try {
stmt.close();
} catch (Exception e) {
logger.error("blobUpdateForOracle", e);
}
}
if (cn != null) {
try {
cn.close();
} catch (Exception e) {
logger.error("blobUpdateForOracle", e);
}
}
}
if (logger.isDebugEnabled()) {
logger.debug("blobUpdateForOracle(String, String, String, String, byte[]) - end"); //$NON-NLS-1$
}
return result;
}
/**
* oracle中導入模板
*
* @param tableName
* @param filedName
* @param id
* @param t
* @throws SQLException
*/
public static int blobUpdateForOracle(DataSource ds, String tableName, String filedName, String whereStr, final InputStream in) throws SQLException {
return blobUpdateForOracle(ds, tableName, filedName, whereStr, new OutputStreamWriter() {
public void write(OutputStream outputStream) throws IOException {
IOUtils.copy(in, outputStream);
}
});
}