andyj2ee

java tec sky

统计

留言簿(4)

activemq

aop

design pattern

other blog

spring

workflow

多线程

软件架构师

阅读排行榜

评论排行榜

Oracle Blob/Clob 字段写入时产生转型异常Cast Exception

CLOB clob = (CLOB)us.getStatisticsInfo();

原因是
java.sql.Blob不能强制传唤成oracle.sql.BLOB

解决方法如下:

SerializableClob lob=(SerializableClob)us.getStatisticsInfo();
CLOB lob2 = (CLOB)lob.getWrappedClob(); 
Writer out = lob2.getCharacterOutputStream(); 
  

public void testAdd() throws Exception {
        
byte[] buffer = new byte[1]; 
        buffer[
0= 1;
        Session s 
= null;
        
try 
            s 
= sf.openSession(); 
            Transaction tx 
= s.beginTransaction();
            BizUserStatistics us
= new BizUserStatistics();
            us.setId(
new Long(100));
            us.setStatisticsInfo(Hibernate.createClob(
" "));

            s.save(us); 
            s.flush(); 
            s.refresh(us, LockMode.UPGRADE); 
//            CLOB clob = (CLOB)us.getStatisticsInfo(); 
            SerializableClob lob=(SerializableClob)us.getStatisticsInfo();
            CLOB lob2 
= (CLOB)lob.getWrappedClob();     
            Writer out 
= lob2.getCharacterOutputStream();
            String fileName 
= "d:/process_log.sql";
            File f 
= new File(fileName); 
            FileInputStream fin 
= new FileInputStream(f);
 
            StringBuffer sb 
= new StringBuffer();
            
for (int i = 0; i < 1000; i++){
                sb.append(
"<data>" + i + "</data>");
            }

            out.write(sb.toString());
            out.close();
            
            s.flush(); 
            tx.commit(); 
            }
 catch (Exception e) {
                e.printStackTrace(); 
            }
 finally {
                
if (s != null)
                
try {
                s.close(); 
                }
 catch (Exception e){}
            }


    }



方向:分布式系统设计

posted on 2006-02-14 16:35 java光环 阅读(1223) 评论(0)  编辑  收藏 所属分类: hibernate


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


网站导航: