如何用JSP處理Oracle中CLOB類型的欄位

來源:互聯網
上載者:User
  1.       CLOB 欄位的插入

clob類型的資料直接insert可能會因為字元過長而出錯,所以要用對象形式插入.

樣本:

首先,建立一個含CLOB欄位的表:

create table test (id INTEGER, content clob);

 

然後插入空值

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

con.setAutoCommit(false);  //conn為Connection對象

//建立並執行個體化一個CLOB對象

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

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

//對CLOB對象賦值

   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('儲存資料時出錯,請檢查你輸入的資料格式是否符合要求');self.history.back ();</script>");

 }

 

注意:

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

 

2.       CLOB類型的修改

 

對CLOB欄位的修改除了sql語句外其他和插入相同

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

con.setAutoCommit(false);  //conn為Connection對象

//建立並執行個體化一個CLOB對象

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

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

//對CLOB對象賦值

   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('儲存資料時出錯,請檢查你輸入的資料格式是否符合要求');self.history.back ();</script>");

 }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.