67,512
社区成员
发帖
与我相关
我的任务
分享
/**
* 将JDom对象转换字符串.
*
* @param document
* 将要被转换的JDom对象
* @param encoding
* 输出字符串使用的编码
* @return String xml对象保存到的字符串
* @throws EMPException
*/
private String OutputToString(Document document)
throws EMPException {
ByteArrayOutputStream byteRep = new ByteArrayOutputStream();
Format format = Format.getPrettyFormat();
format.setEncoding("UTF-8");
XMLOutputter docWriter = new XMLOutputter();
docWriter.setFormat(format);
try {
docWriter.output(document, byteRep);
} catch (Exception e) {
e.printStackTrace();
}
try {
return byteRep.toString("utf-8");
} catch (UnsupportedEncodingException e) {
EMPLog.log(PUBConstant.MODIFY_HISTORY_COMPONENT, EMPLog.ERROR, 0,
"transfer error", e);
throw new EMPException();
}
}
/**
* 把修改记录的XML字符串插入数据库中 PS. sql语句如果使用for update时会有死锁问题,所以使用同步
*
* @param modifyHistory
* @param conn
* @throws EMPException
*/
public synchronized void insertBlobData(ModifyHistory modifyHistory,
Connection conn) throws EMPException {
BufferedOutputStream out = null;
PreparedStatement ps = null;
PreparedStatement ps1 = null;
ResultSet rs = null;
try {
conn.setAutoCommit(false);
// 先插入空的BLOB值
String insertSql = "insert into modify_history(key_id, table_name, cus_id,cus_name,modify_record, modify_time, modify_user_id, modify_user_ip, modify_status, modify_user_br_id,modify_type) values (?, ?, ?, ?, EMPTY_BLOB(), ?, ?, ?, ?, ?, ?)";
ps = conn.prepareStatement(insertSql);
ps.setString(1, modifyHistory.getKeyId());
ps.setString(2, modifyHistory.getTableName());
ps.setString(3, modifyHistory.getCusId());
ps.setString(4, modifyHistory.getCusName());
ps.setString(5, modifyHistory.getModifyTime());
ps.setString(6, modifyHistory.getModifyUserId());
ps.setString(7, modifyHistory.getModifyUserIp());
ps.setString(8, modifyHistory.getModifyStatus());
ps.setString(9, modifyHistory.getModifyUserBrId());
ps.setString(10,( modifyHistory.getModifyType()== null) ? "":modifyHistory.getModifyType());
EMPLog.log(PUBConstant.MODIFY_HISTORY_COMPONENT, EMPLog.WARNING, 0,
"insertSql: " + insertSql);
ps.executeUpdate();
// 从数据库取出该BLOG值,修改这个值
String selectSql = "select modify_record from modify_history where key_id ='"
+ modifyHistory.getKeyId() + "'";
EMPLog.log(PUBConstant.MODIFY_HISTORY_COMPONENT, EMPLog.WARNING, 0,
"selectSql: " + selectSql);
ps1 = conn.prepareStatement(selectSql);
rs = ps1.executeQuery();
if (rs.next()) {
Blob blob = rs.getBlob("modify_record");
blob.setBytes(1, modifyHistory.getModifyRecord().getBytes());
}
conn.commit();
} catch (Exception e) {
try {
conn.rollback();
} catch (SQLException e1) {
}
throw new EMPException(e);
} finally {
try {
if (ps != null) {
ps.close();
}
} catch (SQLException e1) {
}
try {
if (ps1 != null) {
ps1.close();
}
} catch (SQLException e1) {
}
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
}
try {
if (rs != null) {
rs.close();
}
} catch (SQLException e) {
}
}
}