java 存储oracle的clob字段

项目中有很长的字符创需要存储,用到了oracle的clob字段,直接很长的字符串插入到clob字段中会报字符过长的异常,于是便寻求解决方案。看到这个博客写的还不错

首先,创建一个含CLOB字段的表:

create table test (id INTEGER, content clob);

 然后插入空值

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

con.setAutoCommit(false); //connConnection对象

//创建并实例化一个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;      


使用上面的解决方案,在项目会出现问题,因为我用的是c3p0的的数据库连接池获取的数据库连接,于是报了 com.mchange.v2.c3p0.impl.NewProxyConnection cannot be cast to oracle.jdbc.driver.OracleConnection异常,自己尝试了半天又在网上找到了解决方案:
- added spring-1.2-rc1.jar 

- added 
import org.springframework.jdbc.support.nativejdbc.C3P0NativeJdbcExtractor; 

- replaced the following code in nullSafeSet method from JGeometryType: 

OracleConnection connection = (OracleConnection) preparedStatement.getConnection(); 

with: 

C3P0NativeJdbcExtractor cp30NativeJdbcExtractor = new C3P0NativeJdbcExtractor(); 
OracleConnection connection = (OracleConnection) cp30NativeJdbcExtractor.getNativeConnection(preparedStatement.getConnection()); 


希望和我有同样问题的朋友能够少走弯路


原文地址:https://www.cnblogs.com/javawebsoa/p/3223538.html