在POJO中字符串大对象可以声明成一个java.lang.String或java.sql.Clob类型。
当程序从数据库中加载Clob类型数据时,仅仅加载了一个Clob类型的数据的逻辑指针。我们需要通过使用Clob.getCaracterStream()方法得到Clob类型的数据输入流之后才能获取大对象数据。
看下面具体代码
package dao; import java.io.BufferedReader; import java.io.IOException; import java.io.Reader; import java.math.BigDecimal; import java.sql.Clob; import java.sql.SQLException; import org.hibernate.LobHelper; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.Transaction; import entity.Clobtable; import Factory.HibernateSessionFactory; public class ClobDao { private Session session = null; private Transaction tran = null; public ClobDao() { session = HibernateSessionFactory.getSession(); } public void saveClob(BigDecimal id,String content){ Clobtable ct = new Clobtable(); ct.setId(id); LobHelper lh = session.getLobHelper(); ct.setContent(lh.createClob(content)); tran = session.beginTransaction(); try{ session.save(ct); tran.commit(); System.out.println("插入成功!"); }catch(Exception e){ tran.rollback(); System.out.println("插入失败"); } } public void getClob(BigDecimal id){ String hql = "from Clobtable where id = ?"; Query query = session.createQuery(hql); query.setBigDecimal(0, id); Clobtable ct = (Clobtable) query.uniqueResult(); Clob clob = ct.getContent(); try { Reader reader = clob.getCharacterStream(); BufferedReader br = new BufferedReader(reader); String content = br.readLine(); System.out.println(content); } catch (SQLException e) { e.printStackTrace(); System.out.println("读取失败!"); } catch (IOException e) { System.out.println("读取失败!"); } } }
原文地址:http://blog.csdn.net/u011740475/article/details/39399349