JavaSky
          JavaBird
          posts - 8,  comments - 9,  trackbacks - 0
          CLOB字段的插入

          clob類型的數(shù)據(jù)直接insert可能會(huì)因?yàn)樽址^(guò)長(zhǎng)而出錯(cuò),所以要用對(duì)象形式插入.

          示例:

          首先,創(chuàng)建一個(gè)含CLOB字段的表:

          create table test (id INTEGER, content clob);

           

          然后插入空值

          String strSql = “INSERT INTO TEST(id,content) values(‘0001’,?) ”;

          con.setAutoCommit(false); //connConnection對(duì)象

          //創(chuàng)建并實(shí)例化一個(gè)CLOB對(duì)象

          CLOB clob = new CLOB((OracleConnection)con);

             clob = oracle.sql.CLOB.createTemporary((OracleConnection)con,true,1);

          //對(duì)CLOB對(duì)象賦值

             clob.putString(1,formateSingleQuotes(Content));

             OracleConnection OCon = (OracleConnection)con;

             OraclePreparedStatement pstmt = (OraclePreparedStatement)OCon.prepareCall(strSql);

             pstmt.setCLOB(1,clob);

             int i = pstmt.executeUpdate();

             pstmt.close();

             OCon.commit();

             OCon = null;

          con = null;      

          Boolean flag = true;

          if(i <=0)

           {

                flag = false;

           }

           

          if (flag == true)

           {

                 out.print( "<script>alert('成功保存!'); </script>");

          }

           else

           {

                 out.print( "<script>alert('保存數(shù)據(jù)時(shí)出錯(cuò),請(qǐng)檢查你輸入的數(shù)據(jù)格式是否符合要求');self.history.back ();</script>");

           }

           

          注意:

          一定不要忘記引入包:import oracle.sql.*,oracle.jdbc.driver.*

           

          2.       CLOB類型的修改

           

          對(duì)CLOB字段的修改除了sql語(yǔ)句外其他和插入相同

          String strSql = “UPDATE TEST set content=? Where Id=’0001’ ”;

          con.setAutoCommit(false); //connConnection對(duì)象

          //創(chuàng)建并實(shí)例化一個(gè)CLOB對(duì)象

          CLOB clob = new CLOB((OracleConnection)con);

             clob = oracle.sql.CLOB.createTemporary((OracleConnection)con,true,1);

          //對(duì)CLOB對(duì)象賦值

             clob.putString(1,formateSingleQuotes(Content));

             OracleConnection OCon = (OracleConnection)con;

             OraclePreparedStatement pstmt = (OraclePreparedStatement)OCon.prepareCall(strSql);

             pstmt.setCLOB(1,clob);

             int i = pstmt.executeUpdate();

             pstmt.close();

             OCon.commit();

             OCon = null;

          con = null;      

          Boolean flag = true;

          if(i <=0)

           {

                flag = false;

           }

           

          if (flag == true)

           {

                 out.print( "<script>alert('成功保存!'); </script>");

          }

           else

           {

                 out.print( "<script>alert('保存數(shù)據(jù)時(shí)出錯(cuò),請(qǐng)檢查你輸入的數(shù)據(jù)格式是否符合要求');self.history.back ();</script>");

           }

          posted on 2012-01-18 17:15 JavaBird 閱讀(10373) 評(píng)論(2)  編輯  收藏 所屬分類: J2EE

          FeedBack:
          # re: Java處理(插入或修改)clob類型數(shù)據(jù)
          2013-05-22 14:07 | abbey
          垃圾,con怎么來(lái)的?  回復(fù)  更多評(píng)論
            
          # re: Java處理(插入或修改)clob類型數(shù)據(jù)
          2013-09-03 09:15 | jjj
          @abbey
          你個(gè)二貨,con是獲取數(shù)據(jù)庫(kù)連接好吧,這都要寫上去?   回復(fù)  更多評(píng)論
            

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


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

          <2013年9月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          常用鏈接

          留言簿

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          •  

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 临澧县| 米林县| 临城县| 西充县| 乌鲁木齐市| 兰考县| 丹东市| 隆子县| 神池县| 广汉市| 康马县| 五家渠市| 武强县| 星子县| 鸡西市| 乡宁县| 通榆县| 武陟县| 将乐县| 五台县| 冕宁县| 开江县| 徐州市| 满洲里市| 历史| 桂东县| 方正县| 巴楚县| 宁远县| 和顺县| 海安县| 玉林市| 满城县| 招远市| 台北市| 绍兴市| 永康市| 荆州市| 荆门市| 湖南省| 江陵县|