5、Clob
public void add(Cat obj); throws Exception{
Session sess=HibernateUtil.currentSession();;
Transaction tx=sess.beginTransaction();;
obj.setMes(Hibernate.createClob(" "););;
sess.save(obj);;
sess.flush();;
sess.refresh(obj, LockMode.UPGRADE);;
CLOB clob=(CLOB);obj.getMes();;
Writer pw=clob.getCharacterOutputStream();;
pw.write(obj.getMesString(););;
pw.flush();;
pw.close();;
sess.flush();;
tx.commit();;
HibernateUtil.closeSession();;
}
public void add1(Cat obj); throws Exception{
Context ctx=new InitialContext();;
DataSource ds=(DataSource);ctx.lookup("java:comp/env/jdbc/pmis");;
Connection conn=ds.getConnection();;
conn.setAutoCommit(false);;
Statement stmt=conn.createStatement();;
stmt.executeUpdate("update cat set mes=empty_clob(); where id=2079");;
ResultSet rs=stmt.executeQuery("select mes from cat where id=2079 for update");;
if(rs.next(););{
//CLOB clob=((OracleResultSet);rs);.getCLOB("mes");;
CLOB clob=(CLOB);rs.getClob("mes");;
Writer pw=clob.getCharacterOutputStream();;
pw.write(obj.getMesString(););;
pw.flush();;
pw.close();;
}
rs.close();;
stmt.close();;
conn.commit();;
conn.close();;
}
public List lists(); throws Exception{
Session sess=HibernateUtil.currentSession();;
Query query=sess.createQuery("from Cat");;
List list=query.list();;
HibernateUtil.closeSession();;
for(int i=0; i<list.size();; i++);{
Cat obj=(Cat);list.get(i);;
obj.setMesString(obj.getMes();==null?null:obj.getMes();.getSubString(1, (int);obj.getMes();.length();););;
}
return list;
}
package pmis.common.hibAccess;
import net.sf.hibernate.*;
import net.sf.hibernate.cfg.*;
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static
{
try
{
sessionFactory = new Configuration();.configure();.buildSessionFactory();;
}
catch (HibernateException ex);
{
throw new RuntimeException("Exception building SessionFactory: " + ex.getMessage();, ex);;
}
}
public static final ThreadLocal session = new ThreadLocal();;
public static Session currentSession(); throws HibernateException
{
Session s = (Session); session.get();;
// Open a new Session, if this Thread has none yet
//if (s == null);
{
s = sessionFactory.openSession();;
session.set(s);;
}
return s;
}
public static void closeSession(); throws HibernateException
{
Session s = (Session); session.get();;
session.set(null);;
if (s != null);
{
s.close();;
}
}
}
package test;
import pmis.common.hibAccess.HibernateUtil;
import javax.sql.DataSource;
import javax.naming.InitialContext;
import javax.naming.Context;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Connection;
import java.io.Writer;
import net.sf.hibernate.Session;
import ammeter.valueObject.TsBmRelation;
import oracle.sql.CLOB;
/**
* Created by IntelliJ IDEA.
* User: zpt
* Date: 2005-1-26
* Time: 9:03:54
* To change this template use File | Settings | File Templates.
*/
public class test {
public test(); throws Exception{
t2();;
}
private void t1(); throws Exception{
Context iniContext=new InitialContext();;
Context envContext=(Context);iniContext.lookup("java:comp/env");;
DataSource ds=(DataSource);envContext.lookup("jdbc/pmis/oracle");;
Connection conn=ds.getConnection();;
Statement stmt=conn.createStatement();;
ResultSet rs=stmt.executeQuery("select name from Cat");;
String s="";
while(rs.next(););{
s+=rs.getString("name");;
}
rs.close();;
stmt.close();;
conn.close();;
}
private void t2(); throws Exception{
Session sess=HibernateUtil.currentSession();;
TsBmRelation obj=(TsBmRelation);sess.get(TsBmRelation.class, new Long(1971););;
HibernateUtil.closeSession();;
}
private void t3(); throws Exception{
net.sf.hibernate.HibernateException
Context iniContext=new InitialContext();;
Context envContext=(Context);iniContext.lookup("java:comp/env");;
DataSource ds=(DataSource);envContext.lookup("jdbc/pmis/oracle");;
Connection conn=ds.getConnection();;
conn.setAutoCommit(false);;
Statement stmt=conn.createStatement();;
stmt.executeUpdate("update cat set mes=empty_clob(); where id=2148");;
ResultSet rs=stmt.executeQuery("select mes from cat where id=2148 for update");;
if(rs.next(););{
//CLOB clob=((OracleResultSet);rs);.getCLOB("mes");;
CLOB clob=(CLOB);rs.getClob("mes");;
Writer pw=clob.getCharacterOutputStream();;
pw.write("asdfasdf");;
pw.flush();;
pw.close();;
}
rs.close();;
stmt.close();;
conn.commit();;
conn.close();;
}
private void t4(); throws Exception{
Session sess=HibernateUtil.currentSession();;
HibernateUtil.closeSession();;
}
}