如何用JSP处理Oracle中CLOB类型的字段
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>");
}