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 閱讀(1960) 評論(1)  編輯  收藏

          評論

          # re: Hibernate3 存取Oracle數據庫 CLOB大對象 2012-05-28 10:16 輔導輔導
          輔導輔導輔導輔導輔導輔導費  回復  更多評論
            


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 防城港市| 黄骅市| 海城市| 靖边县| 沁水县| 江孜县| 新民市| 宜都市| 治多县| 莒南县| 奇台县| 大城县| 石景山区| 包头市| 潜江市| 河源市| 祁阳县| 天全县| 益阳市| 仁寿县| 泉州市| 响水县| SHOW| 东乡族自治县| 舟山市| 上犹县| 平安县| 吉安市| 迁西县| 陆丰市| 新巴尔虎左旗| 平定县| 德格县| 友谊县| 枣阳市| 淳化县| 宁武县| 湘阴县| 宾川县| 皮山县| 陵水|