| 導購 | 订阅 | 在线投稿
分享
 
 
 

如何用JSP處理Oracle中CLOB類型的字段

2007-02-07 12:19:04  編輯來源:互聯網  简体版  手機版  移動版  評論  字體: ||

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>");

}

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>"); }
󰈣󰈤
王朝萬家燈火計劃
期待原創作者加盟
 
 
 
>>返回首頁<<
 
 
 
 
 熱帖排行
 
王朝網路微信公眾號
微信掃碼關註本站公眾號 wangchaonetcn
 
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有