Rising Sun

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  148 随笔 :: 0 文章 :: 22 评论 :: 0 Trackbacks

[http://blog.javascud.org/rss.php?blogId=25&categoryId=32]
测试类 ClobTest.java

/**
 *
 */
package com.chinantn.test;

import java.io.Writer;
import java.sql.Clob;

import oracle.sql.CLOB;

import org.hibernate.Hibernate;
import org.hibernate.LockMode;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.lob.SerializableClob;

import com.chinantn.sdfda.common.hibernate.HibernateSessionFactory;
import com.chinantn.sdfda.content.domain.Test;

/**
 * @author Administrator
 *
 */
public class ClobTest {

 /**
  * @param args
  */
 public static void main(String[] args) throws Exception{
  Session s = HibernateSessionFactory.currentSession();
  /*//写CLOB
  Transaction tx = s.beginTransaction();
  Test t = new Test();
  t.setContent(Hibernate.createClob(" "));
  s.save(t);
  s.flush();
  s.refresh(t,LockMode.UPGRADE);
  //CLOB clob = (CLOB)t.getContent();
  
  SerializableClob sc= (SerializableClob)t.getContent();
  Clob wrapclob = sc.getWrappedClob();
  CLOB clob = (CLOB)wrapclob;
  
  
// Writer cout = clob.getCharacterOutputStream();
//用文件上传到 clob 字段是乱码 (中文)//
// File file = new File("C:\\1.txt");//修改你要存如的文本
// FileInputStream fin = new FileInputStream(file);
//int read;
//while((read = fin.read())!= -1){
 //        cout.write(read);
 / }
 //fin.close();
//
//下面是正常
  StringBuffer sb = new StringBuffer();
  for(int i = 0; i < 10000; i ++){
   sb.append("我要忍!!!");
  }
  w.write(sb.toString());
  w.flush();
  w.close();
  tx.commit();*/
  /*
  //读CLOB
  Test t = (Test)s.get(Test.class,"109a1b7438[142bece]-8000");
  Clob clob = t.getContent();
  if(clob != null){
   String clobStr = clob.getSubString(1,(int)clob.length());
   System.out.println(clobStr.length());
   System.out.println(clobStr);
  }*/
  
  //更新CLOB
  Transaction tx = s.beginTransaction();
  Test t = (Test)s.get(Test.class,"109a1b7438[142bece]-8000");
  t.setContent(Hibernate.createClob(" "));
  s.update(t);
  s.flush();
  s.refresh(t,LockMode.UPGRADE);
  
  SerializableClob sc= (SerializableClob)t.getContent();
  Clob wrapclob = sc.getWrappedClob();
  CLOB clob = (CLOB)wrapclob;
  
  Writer w = clob.getCharacterOutputStream();
  StringBuffer sb = new StringBuffer();
  for(int i = 0; i < 1000; i ++){
   sb.append("我很努力!!!");
  }
  w.write(sb.toString());
  w.flush();
  w.close();
  tx.commit();
  
  if(clob != null){
   String clobStr = clob.getSubString(1,(int)clob.length());
   System.out.println(clobStr.length());
   System.out.println(clobStr);
  }
  HibernateSessionFactory.closeSession();

 }

}

posted on 2007-08-13 09:58 brock 阅读(1956) 评论(1)  编辑  收藏

评论

# re: Hibernate3 存取Oracle数据库 CLOB大对象 2012-05-28 10:16 辅导辅导
辅导辅导辅导辅导辅导辅导费  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: