Natural

           

          Oracle中插入Clob大字段時(shí)的處理

          最近一次開發(fā)中涉及clob字段的存儲(chǔ)。以前沒(méi)使用過(guò),結(jié)果發(fā)現(xiàn)它不能像普通的varchar2、date等字段那樣存儲(chǔ)。上網(wǎng)查詢了一下,結(jié)果發(fā)現(xiàn)有些資料不太準(zhǔn)確。最后找了個(gè)能用的解決。特此寫個(gè)demo與大家分享。(PS:測(cè)試時(shí)用的是Oracle9i)

          import java.io.Writer;
          import java.sql.Clob;
          import java.sql.Connection;
          import java.sql.PreparedStatement;
          import java.sql.ResultSet;
          import java.sql.Statement;

          import oracle.jdbc.driver.OracleResultSet;
          import oracle.sql.CLOB;

          public class Test {

              
          /**
               * 更新Clob字段
               * 
          @param sourceConn Connection 源數(shù)據(jù)庫(kù)的連接詞
               * 
          @param targetConn Connection 目標(biāo)數(shù)據(jù)庫(kù)的連接詞
               
          */
              
          public void updateClob(Connection sourceConn, Connection targetConn)
              {
                  ResultSet rsSource 
          = null;
                  ResultSet oracleRS 
          = null;
                  Connection connTarget 
          = null;    // 目標(biāo)數(shù)據(jù)庫(kù)的連接
                  Connection connSource = null;    // 源數(shù)據(jù)庫(kù)的連接
                  PreparedStatement psmtInsert = null;
                  PreparedStatement psmtSelect 
          = null;
                  Statement oracleStmt 
          = null;
                  
                  String sInsertSQL 
          = "INSERT INTO tableTarget(id,context) VALUES (?,empty_clob())";
                  String sSelectSQL 
          = "SELECT context FROM tableSource WHERE id = ? ";
                  String sID 
          = "1";
                  
                  connTarget 
          = targetConn; // 獲取數(shù)據(jù)庫(kù)連接
                  connSource = sourceConn; // 
                  
                  
          // 查詢?cè)磾?shù)據(jù)庫(kù)中表tableSource的clob字段context
                  psmtSelect = connSource.prepareStatement(sSelectSQL);
                  psmtSelect.setString(
          1, sID);
                  psmtSelect.executeQuery();
                  rsSource 
          = psmtSelect.getResultSet();
                  
                  
          // 將clob字段插入目標(biāo)數(shù)據(jù)庫(kù)中的表tableTarget
                  psmtInsert = connTarget.prepareStatement(sInsertSQL);
                  connTarget.setAutoCommit(
          false);    // 設(shè)置為不即時(shí)提交,待會(huì)后面一起提交
                  psmtInsert.setString(1, sID);
                  psmtInsert.executeUpdate();
                  
                  
          // 更新CLOB字段
                  Clob clobSource = rsSource.getClob("context");    // 源數(shù)據(jù)庫(kù)的Clob字段
                  String sClob = clobSource.getSubString((long)1, (int)clobSource.length());
                  CLOB clobTarget 
          = null;            // 目標(biāo)數(shù)據(jù)庫(kù)的字段
                  oracleStmt = connTarget.createStatement();
                  oracleRS 
          = oracleStmt.executeQuery("SELECT context FROM tableTarget  WHERE id = '"+sID+"'");
                  
                  
          // 寫入Clob字段
                  if(oracleRS.next() && sClob != null)
                  {
                      clobTarget 
          = ((OracleResultSet)oracleRS).getCLOB("context");
                      Writer writer 
          = clobTarget.getCharacterOutputStream() ;
                      writer.write(sClob);
                      writer.flush();
                      writer.close();
                      
                      connTarget.commit();    
          // 事務(wù)提交
                  }
              }
              
          }


          posted on 2009-10-20 10:13 此號(hào)已被刪 閱讀(2866) 評(píng)論(0)  編輯  收藏 所屬分類: Oracle


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


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

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿(8)

          隨筆分類(83)

          隨筆檔案(78)

          文章檔案(2)

          相冊(cè)

          收藏夾(7)

          最新隨筆

          搜索

          積分與排名

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 乃东县| 皮山县| 阿城市| 长汀县| 郯城县| 施秉县| 澎湖县| 红河县| 和田县| 顺义区| 宽城| 清徐县| 湘潭市| 炎陵县| 新津县| 石首市| 新乡市| 穆棱市| 朝阳区| 灵台县| 万源市| 江口县| 滁州市| 印江| 平阴县| 且末县| 军事| 金乡县| 久治县| 广昌县| 拉萨市| 策勒县| 平谷区| 万荣县| 武陟县| 安庆市| 海阳市| 扶沟县| 张家界市| 保山市| 平武县|