日期:2014-05-16  浏览次数:20513 次

oracle数据库里保存clob字段
public void addStatements(StatementsBean statementsBean) {
try {
Session session = this.getSession();
    Transaction tran=session.beginTransaction();
    statementsBean.setStatementsContent(Hibernate.createClob(" "));//注意,这里的参数是个空格,先新增一个空的Clob进去
    session.save(statementsBean);
    session.flush();//强制执行
    session.refresh(statementsBean,LockMode.UPGRADE);

    SerializableClob sc=(SerializableClob)statementsBean.getStatementsContent();//kybasicInfo.getInfoContent()是Clob类型的
    Clob wrapclob=sc.getWrappedClob();//这里的Clob是java.sql.Clob
    CLOB clob=(CLOB)wrapclob;//这里的CLOB是oracle.sql.CLOB
    Writer writer=clob.getCharacterOutputStream();
    writer.write(statementsBean.getContentToString());//kybasicInfo.getInfoContentToString()是String类型的,在action里就是传这个进来,然后再通过文件流形式写成CLOB字段中
    writer.close();

    session.save(statementsBean);
    tran.commit();
  
   } catch (RuntimeException re) {
    throw re;
   } catch (SQLException e) {
    e.printStackTrace();
   } catch (IOException e) {
    e.printStackTrace();
   }



}