/**
* 读 字段 clob
* */
public static String oracleClob2Str(java.sql.Clob clob) throws Exception {
return (clob != null ? clob.getSubString(1, (int) clob.length()) : null);
}
注意是上述方法的参数类型是这个接口:java.sql.Clob,它向下兼容MSSQL的 net.sourceforge.jtds.jdbc.ClobImpl 以及ORACLE的
oracle.sql.CLOB 这两个实现类(具体的子类)。所以该方法可用于多种数据库的Clob字段的读值操作。
// 写字段前需要先将 要写值的Clob 字段进行置空操作,可以像下面给出的sql 语句那样进行操作
// SqlString sql = "update t_information set content=empty_clob()";
/**
* 写ORACLE数据库大字段 clob
* */
public static void oracleStr2Clob(Connection mConn,int id,String content) {
try {
boolean commitStatus = mConn.getAutoCommit();
mConn.setAutoCommit(false);
PreparedStatement pstmt = null;
pstmt = mConn
.prepareStatement("SELECT content FROM t_information WHERE id=? FOR UPDATE ");
pstmt.setInt(1, id);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
oracle.sql.CLOB clob = (oracle.sql.CLOB)rs.getClob(1);
Writer writer = clob.getCharacterOutputStream();
char[] charContent = content.toCharArray();
java.io.CharArrayReader aCharReader = new CharArrayReader(
charContent);
int c;
while ((c = aCharReader.read()) > -1) {
writer.write(c);
}
//writer.write(content);
writer.flush();
writer.close();
aCharReader.close();
mConn.commit();
mConn.setAutoCommit(commitStatus);
}
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}