这是我发现的一些代码,我认为它可以满足您的需求:
我有一个像这样的静态方法:
public class DBUtil {
public static CLOB getCLOB(String innStr, Connection conn) throws SQLException,
IOException {
CLOB tempClob = null;
// If the temporary CLOB has not yet been created, create new
tempClob = CLOB.createTemporary(conn, true, CLOB.DURATION_SESSION);
// Open the temporary CLOB in readwrite mode to enable writing
tempClob.open(CLOB.MODE_READWRITE);
// Get the output stream to write
Writer tempClobWriter = tempClob.getCharacterOutputStream();
// Write the data into the temporary CLOB
tempClobWriter.write(innStr);
// Flush and close the stream
tempClobWriter.flush();
tempClobWriter.close();
// Close the temporary CLOB
tempClob.close();
return tempClob;
}
}
然后你可以像这样使用它:
CLOB tempClob = DBUtil.getCLOB(longString, connection);
pstmt.setCLOB(colnumber, tempClob);
这要求整个 clob 内容都在一个字符串中,所以它都加载到内存中,也许不适合你,但它一直在满足我的简单需求。
编辑:我可能应该注意到我将此代码与 plsql 过程调用一起使用。没有直接用sql测试过