幾年前的代碼,做一下記錄
/**
* <將前段傳入的string對象轉換爲Clob對象>
*@author liuzhu
*@param str
*@return
*/
private Clob stringToClob(String str) {
if (null == str)
return null;
else {
try {
java.sql.Clob c = new javax.sql.rowset.serial.SerialClob(str
.toCharArray());
return c;
} catch (Exception e) {
return null;
}
}
}
public long saveCheckerTrainHistory(final CheckerTrainHistoryVO vo,final String trainingSummary)
{
long retVal = (Long)getZormTemplate().execute(new ZormCallback()
{
@Override
public Long doInZorm(Session session) throws ZormException, SQLException
{
session.beginTransaction();
Connection conn = session.connection();
StringBuffer sql = new StringBuffer();
sql.append(" INSERT INTO CHECKER_TRAINING_HISTORY ( ");
sql.append(" oid, store_id,checkerid,training_theme, ");
sql.append(" training_employees, training_summary, ");
sql.append(" training_date,created_by, created_time, ");
sql.append(" last_updated_by,last_updated_time ) ");
sql.append(" values(?,?,?,?,?,empty_clob(),?,?,?,?,?)");
//先插入一個空的Clob數據
PreparedStatement ps = null;
ResultSet rst = null;
try
{
CLOB clob=null;
long rowNum = 0;
//禁用自動提交事務
conn.setAutoCommit(false);
String seqSql = "select checker_training_history_s.nextval from dual";
ps=conn.prepareStatement(seqSql.toString());
rst = ps.executeQuery();
if(rst.next()){
rowNum = rst.getLong(1);
}
ps.close();
ps=conn.prepareStatement(sql.toString());
ps.setLong(1, rowNum);
ps.setLong(2, vo.getStoreId());
ps.setLong(3, vo.getCheckerId());
ps.setString(4, vo.getTrainingTheme());
ps.setLong(5, vo.getTrainingEmployees());
ps.setDate(6, new java.sql.Date(vo.getTrainingDate().getTime()));
ps.setLong(7, vo.getCreatedBy());
ps.setDate(8, new java.sql.Date(vo.getTrainingDate().getTime()));
ps.setLong(9, vo.getLastUpdatedBy());
ps.setDate(10, new java.sql.Date(vo.getTrainingDate().getTime()));
ps.executeUpdate();
ps.close();
//查詢並獲得這個cursor,並且加鎖
String sql2=" select training_summary from checker_training_history where oid=? for update";
ps=conn.prepareStatement(sql2);
ps.setLong(1, rowNum);
rst = ps.executeQuery();
if(rst.next()){
clob=(CLOB)rst.getClob(1);
}
/* 向CLOB對象中寫入數據 */
BufferedWriter out = new BufferedWriter(clob.getCharacterOutputStream());
out.write(trainingSummary);
//一定要flush(),否則不會將String對象寫入到Clob當中去。。。
out.flush();
out.close();
ps.close();
//更新clob對象
String sql3="update checker_training_history set training_summary =? where oid=?";
ps=conn.prepareStatement(sql3);
ps.setClob(1, clob);
ps.setLong(2, rowNum);
ps.executeUpdate();
ps.close();
conn.commit();
conn.setAutoCommit(true);
return rowNum;
}
catch (SQLException|IOException e)
{
e.printStackTrace();
}
finally
{
try
{
if(ps!=null)
{
ps.close();
}
if(rst != null)
{
rst.close();
}
if(conn != null)
{
conn.close();
}
}
catch (SQLException e)
{
e.printStackTrace();
}
}
session.clear();
return 0L;
}
});
return retVal;
}