[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();
}
}