Oracle 中Clob 类型数据的处理

public  void InsertContents(MContent content)
throws Exception {
String Title=content.getTitle() ;
String contents=content.getContents();
Connection sqlConn = null;
sqlConn = getConnection();
sqlConn.setAutoCommit(false); // *
//id, column_code, title, createdate, contenturl, contents
String sql = "insert into M_Content (column_code, title, createdate, contenturl, contents,filePath )values('"+content.getColumnCode()+
"','"+content.getTitle()+"',default,'"+content.getContentUrl()+ "',empty_clob(),'"+content.getFilePath()+"')";
System.out.println(sql);
Statement stmt = sqlConn.createStatement();
stmt.executeUpdate(sql);

sqlConn.commit(); // *
String strSQL = "select ID from M_Content where title='" + Title
+ "' and ROWNUM<=1 order by ID DESC";
System.out.println(strSQL);
String[][] array = ShareProcedure.getResultSet(strSQL);
if (array != null) {
sql = "select contents from M_Content where id=" + array[0][0]
+ " for update ";
System.out.println(sql);
ResultSet rs = stmt.executeQuery(sql);
if (rs.next()) {
java.sql.Clob clob = rs.getClob(1);
clob.setString(1, contents);
sql = "update M_Content set contents=? where id=" + array[0][0];
    System.out.println(sql);
PreparedStatement pstmt = sqlConn.prepareStatement(sql);
pstmt.setClob(1, clob);
pstmt.executeUpdate();
sqlConn.commit();
}
}
stmt.close();
sqlConn.close();
System.out.println("content insert successful!");

}



public boolean update(MContent content){
   
String sqlUpdate=" update M_Content set contents=empty_clob() where id="+content.getId();

    String sql = "select contents from M_Content where id=" +content.getId()+ " for update ";
Connection con=pubecti.ShareProcedure.getConnection();
try {
ps=con.prepareStatement(sql);
Statement st=con.createStatement();
st.executeUpdate(sqlUpdate);
con.commit();

ResultSet rs = ps.executeQuery(sql);
if(rs.next()) {
java.sql.Clob clob = null;


clob = rs.getClob(1);
if(content.getContents()!=null){
clob.setString(1, content.getContents());
}else{
clob.setString(1, "");
}
String strSql="update M_Content set title=?,column_code=?,contenturl=?,contents=? ,filePath=? where id="+content.getId();
System.out.println("MContentDAO Update   sql is:"+strSql);
ps=con.prepareStatement(strSql.toString());//PreparedStatement ps=null;
ps.setString(1, content.getTitle());
ps.setString(2, content.getColumnCode());
ps.setString(3, content.getContentUrl());
ps.setClob(4, clob);
ps.setString(5, content.getFilePath());
ps.executeUpdate();
con.commit();
ps.close();
st.close();
rs.close();
con.close();
return true; 
}
}catch(SQLException e){
log.error("MContentDAO Update  () 操作失败"+e.toString());
return false;
}catch(Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return false;
}

你可能感兴趣的:(oracle,sql)