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( "");
}else
{
out.print( "");
}
注意:
一定不要忘記引入包: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( "");
}else
{
out.print( "");
}