David.Turing's blog

           

          Oracle Blob字段寫入時產(chǎn)生轉(zhuǎn)型異常Cast Exception

          我用Hibnernate(JDBC太麻煩了)寫圖片到Blob字段,產(chǎn)生轉(zhuǎn)型異常,
                 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();

          關(guān)于此問題在JavaEye上有一篇文章討論,原因是
          java.sql.Blob不能強(qiáng)制傳喚成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 閱讀(2012) 評論(2)  編輯  收藏

          評論

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

          同樣,對Clob,也是這樣轉(zhuǎn)型

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

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

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

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

          jndi 下的轉(zhuǎn)換
          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);
          }  回復(fù)  更多評論   


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


          網(wǎng)站導(dǎo)航:
           

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿(110)

          我參與的團(tuán)隊(duì)

          隨筆分類(126)

          隨筆檔案(155)

          文章分類(9)

          文章檔案(19)

          相冊

          搜索

          積分與排名

          最新隨筆

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 平果县| 祁门县| 昌黎县| 伊金霍洛旗| 临泽县| 潮安县| 三门峡市| 湖南省| 寻乌县| 镇巴县| 张家口市| 西青区| 赤壁市| 迭部县| 天台县| 漯河市| 安庆市| 华安县| 和林格尔县| 东乌珠穆沁旗| 平潭县| 库车县| 邵东县| 苍溪县| 香格里拉县| 洛扎县| 陕西省| 施秉县| 女性| 深州市| 安塞县| 宁国市| 古浪县| 广南县| 聂拉木县| 秦皇岛市| 武胜县| 岑巩县| 祁连县| 雅安市| 平果县|