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光環 閱讀(1242) 評論(0)  編輯  收藏 所屬分類: hibernate

          主站蜘蛛池模板: 新丰县| 贵德县| 绥滨县| 车致| 墨脱县| 廉江市| 宝清县| 崇州市| 天全县| 永安市| 金阳县| 饶阳县| 松阳县| 色达县| 苗栗市| 浑源县| 嘉祥县| 三亚市| 大新县| 丽江市| 安宁市| 同德县| 渭南市| 青神县| 卢氏县| 闸北区| 滦平县| 宜宾县| 呼玛县| 正宁县| 正阳县| 乡宁县| 马山县| 康平县| 南丹县| 阳泉市| 泉州市| 彭阳县| 邯郸县| 盐城市| 罗江县|