小閣飛空 一池碧映垂楊路 絳云深處 聽盡瀟瀟雨
          At times , people will simply not come through for you in the way you need.Forgive them and move on.
          posts - 212,comments - 87,trackbacks - 0
          在Oracle中,LOB(Large Object,大型對象)類型的字段現在用得越來越多了。因為這種類型的字段,容量大(最多能容納4GB的數據),且一個表中可以有多個這種類型的字段,很靈活,適用于數據量非常大的業務領域(如圖象、檔案等)。而LONG、LONG RAW等類型的字段,雖然存儲容量也不小(可達2GB),但由于一個表中只能有一個這樣類型的字段的限制,現在已很少使用了。

          LOB類型分為BLOB和CLOB兩種:BLOB即二進制大型對象(Binary Large Object),適用于存貯非文本的字節流數據(如程序、圖象、影音等)。而CLOB,即字符型大型對象(Character Large Object),則與字符集相關,適于存貯文本型的數據(如歷史檔案、大部頭著作等)。

          對于一般的java應用程序,clob類型的數據用對象形式插入即可,連接方式采用Oracle Thin JDBC Driver
          注意:存取操作開始前,必須用setAutoCommit(false)取消自動提交,否則Oracle將拋出“讀取違反順序”的錯誤。

          取: ...
                  con.setAutoCommit(false);
          ....
                  if(rss.next())
                  {
                    Clob clob = rss.getClob(1);
                    if(clob!=null)
             //直接轉換為String返回,也可使用getCharacterOutputStream()或getAsciiOutputStream()
             //
             String str=clob.getSubString((long) 1, (int) clob.length());
                 }
                
          寫:LOB類型字段,則只能用SELECT … FOR UPDATE命令將記錄查詢出來并鎖定,然后才能修改
          import oracle.sql.CLOB;
          OracleResultSet rss=(OracleResultSet)stmt.executeQuery("select my_clob from test where id=1 for update"); 
          if(rss.next())
            {
                 CLOB clob=(CLOB)rss.getClob(1);
          //content為大于4k 的String,當然也可使用流形式寫入
                 clob.putString(1,content);
                .....
                OraclePreparedStatement pstmt=(OraclePreparedStatement)con.prepareStatement("update test set my_clob=?");
                pstmt.setClob(1,(Clob)clob);
                  pstmt.executeUpdate();
          ...
          }


          但同樣的方法在BEA WLS 8.1SP2上取數據沒有問題,寫CLOB會有問題的,會報一種這樣的錯誤:java.lang.ClassCastException
          對此bea 是這樣解釋并解決的:
          For most extensions that Oracle provides, you can use the standard technique as described in Using Vendor Extensions to JDBC Interfaces. However, the Oracle Thin driver does not provide public interfaces for its extension methods in the following classes:

          oracle.sql.ARRAY
          oracle.sql.STRUCT
          oracle.sql.REF
          oracle.sql.BLOB
          oracle.sql.CLOB
          WebLogic Server provides its own interfaces to access the extension methods for those classes:

          weblogic.jdbc.vendor.oracle.OracleArray
          weblogic.jdbc.vendor.oracle.OracleStruct
          weblogic.jdbc.vendor.oracle.OracleRef
          weblogic.jdbc.vendor.oracle.OracleThinBlob
          weblogic.jdbc.vendor.oracle.OracleThinClob

          意思即是采用weblogic.jdbc.vendor.oracle.OracleThinClob 替代oracle.sql.CLOB

          修改后的寫CLOB字段,只用修改一句,其他不變。
          weblogic.jdbc.vendor.oracle.OracleThinClob  clob=(OracleThinClob)rss.getClob(1);
          posted on 2005-11-16 14:35 瀟瀟雨 閱讀(1100) 評論(1)  編輯  收藏 所屬分類: JAVADatabase

          FeedBack:
          # re: oracle數據庫中CLOB字段的處理問題
          2006-09-25 15:01 | 姚文杰
          呵呵,我就剛好遇到這個問題,萬分感謝你的文章
            回復  更多評論
            
          主站蜘蛛池模板: 新兴县| 晴隆县| 鄂尔多斯市| 东乡族自治县| 青田县| 金昌市| 普陀区| 天柱县| 高碑店市| 通城县| 内丘县| 虹口区| 凤阳县| 会泽县| 沧源| 寿阳县| 桐庐县| 吉首市| 延长县| 钦州市| 舞钢市| 卫辉市| 颍上县| 石景山区| 依安县| 云南省| 邵阳县| 鄢陵县| 陈巴尔虎旗| 长沙市| 临城县| 新田县| 双鸭山市| 芒康县| 通河县| 三门峡市| 玛曲县| 台前县| 嘉黎县| 和田市| 胶南市|