数据库
数据库存储文本:clob
public void insertClob(int id,String model,String data){
Writer outStream = null;
//插入一个空对象empty_clob(),这个是必须的
String sql="INSERT into MU_MODEL_MAP VALUES( " + id + ",'" + model +"',empty_clob())";
Session session = this.getSessionFactory().openSession();
SQLQuery query = session.createSQLQuery(sql);
query.executeUpdate();
//锁定数据行进行更新,注意“for update”语句,这里不用for update锁定不可以插入clob
String updataSql = "select MODEL_MAP from MU_MODEL_MAP where ID= " + id + " for update";
SQLQuery query1 = session.createSQLQuery(updataSql);
List rs = query1.list();
try{
if(rs.size() > 0){
SerializableClobProxy proxy = (SerializableClobProxy )Proxy.getInvocationHandler(rs.get(0));
oracle.sql.CLOB clob = (CLOB) proxy.getWrappedClob();
outStream = clob.getCharacterOutputStream();
//data是传入的字符串,定义:String data
char[] c = data.toCharArray();
outStream.write(c, 0, c.length);
}
outStream.flush();
outStream.close();
} catch(Exception e){
e.printStackTrace();
}
session.close();
}