David.Turing's blog

           

          Oracle Blob字段寫入時產生轉型異常Cast Exception

          我用Hibnernate(JDBC太麻煩了)寫圖片到Blob字段,產生轉型異常,
                 Configuration config =  new Configuration().configure();
          //       config.addClass(TSealTemplate.class);
                 SessionFactory sf= config.buildSessionFactory();
                  //SessionFactory sf = HibernateSessionFactory.getSessionFactory();
                  s = sf.openSession(); 
                  Transaction tx = s.beginTransaction();
                  TSealTemplate c = new TSealTemplate();
                  c.setUserid("USER0001");
                  c.setSealTemplBlob(Hibernate.createBlob(buffer));
                  s.save(c);
                  s.flush();
                  s.refresh(c, LockMode.UPGRADE);

                 BLOB blob = (BLOB) c.getSealTemplBlob();

          關于此問題在JavaEye上有一篇文章討論,原因是
          java.sql.Blob不能強制傳喚成oracle.sql.BLOB

          解決方法如下:


                  SerializableBlob blob=(SerializableBlob)c.getSealTemplBlob();
                  BLOB blob2 = (BLOB)blob.getWrappedBlob();    
                  OutputStream out = blob2.getBinaryOutputStream();   

          posted on 2006-02-07 13:29 david.turing 閱讀(2011) 評論(2)  編輯  收藏

          評論

          # re: Oracle Blob字段寫入時產生轉型異常Cast Exception 2006-02-09 15:19 david.turing

          同樣,對Clob,也是這樣轉型

          SerializableClob clob=(SerializableClob)m_sig.getSignValue();

          CLOB clob2 = (CLOB)clob.getWrappedClob();

          Writer out = clob2.getCharacterOutputStream();
          out.write(sig);
          out.close();  回復  更多評論   

          # re: Oracle Blob字段寫入時產生轉型異常Cast Exception 2009-09-19 14:31 ccccc

          jndi 下的轉換
          public int setBytes(long pos, byte[] bytes) throws SQLException
          {
          if (blob != null)
          {
          if (blob.getClass().getName().toLowerCase().indexOf("weblogic") >= 0)
          {
          try
          {
          Method method = blob.getClass().getMethod("putBytes",new Class[]{long.class,byte[].class});

          return ((Integer)method.invoke(blob, new Object[]{pos, bytes})).intValue();
          }
          catch (Exception e)
          {
          e.printStackTrace();
          }
          }
          }

          //[weblogic]
          // if (blob instanceof weblogic.jdbc.vendor.oracle.OracleThinBlob)
          // {
          // weblogic.jdbc.vendor.oracle.OracleThinBlob oBlob = (weblogic.jdbc.vendor.oracle.OracleThinBlob) blob;
          // return oBlob.putBytes(pos, bytes);
          // }
          //[endweblogic]
          //[websphere]

          if (blob instanceof oracle.sql.BLOB)
          {
          oracle.sql.BLOB oBlob = (oracle.sql.BLOB) blob;
          return oBlob.putBytes(pos, bytes);
          }
          //[endwebsphere]

          return this.blob.setBytes(pos, bytes);
          }  回復  更多評論   


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


          網站導航:
           

          導航

          統計

          常用鏈接

          留言簿(110)

          我參與的團隊

          隨筆分類(126)

          隨筆檔案(155)

          文章分類(9)

          文章檔案(19)

          相冊

          搜索

          積分與排名

          最新隨筆

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 姚安县| 阿拉善右旗| 乌什县| 梅河口市| 突泉县| 新乐市| 崇信县| 无棣县| 贵定县| 稷山县| 南充市| 都兰县| 四子王旗| 东乌珠穆沁旗| 革吉县| 曲水县| 天等县| 广安市| 双柏县| 青川县| 青浦区| 柯坪县| 丹寨县| 邵东县| 明水县| 榕江县| 扎兰屯市| 鄂温| 潍坊市| 文化| 建始县| 姜堰市| 灵山县| 建平县| 格尔木市| 微博| 枣庄市| 万宁市| 湛江市| 醴陵市| 营山县|