importjava.io.Writer;importjava.sql.Clob;importjava.sql.Connection;importjava.sql.PreparedStatement;importjava.sql.ResultSet;importjava.sql.Statement;importoracle.jdbc.driver.OracleResultSet;importoracle.sql.CLOB;publicclassTest {/*** 更新Clob字段
*@paramsourceConn Connection 源数据库的连接词
*@paramtargetConn Connection 目标数据库的连接词*/publicvoidupdateClob(Connection sourceConn, Connection targetConn)
{
ResultSet rsSource=null;
ResultSet oracleRS=null;
Connection connTarget=null;//目标数据库的连接Connection connSource=null;//源数据库的连接PreparedStatement psmtInsert=null;
PreparedStatement psmtSelect=null;
Statement oracleStmt=null;
String sInsertSQL="INSERT INTO tableTarget(id,context) VALUES (?,empty_clob())";
String sSelectSQL="SELECT context FROM tableSource WHERE id = ?";
String sID="1";
connTarget=targetConn;//获取数据库连接connSource=sourceConn;查询源数据库中表tableSource的clob字段contextpsmtSelect=connSource.prepareStatement(sSelectSQL);
psmtSelect.setString(1, sID);
psmtSelect.executeQuery();
rsSource=psmtSelect.getResultSet();//将clob字段插入目标数据库中的表tableTargetpsmtInsert=connTarget.prepareStatement(sInsertSQL);
connTarget.setAutoCommit(false);//设置为不即时提交,待会后面一起提交psmtInsert.setString(1, sID);
psmtInsert.executeUpdate();//更新CLOB字段Clob clobSource=rsSource.getClob("context");//源数据库的Clob字段String sClob=clobSource.getSubString((long)1, (int)clobSource.length());
CLOB clobTarget=null;//目标数据库的字段oracleStmt=connTarget.createStatement();
oracleRS=oracleStmt.executeQuery("SELECT context FROM tableTarget WHERE id = '"+sID+"'");//写入Clob字段if(oracleRS.next()&&sClob!=null)
{
clobTarget=((OracleResultSet)oracleRS).getCLOB("context");
Writer writer=clobTarget.getCharacterOutputStream() ;
writer.write(sClob);
writer.flush();
writer.close();
connTarget.commit();//事务提交}
}
}