如何使用JDBC更新Oracle Clob

时间:2011-11-15 15:38:52

标签: sql database oracle oracle11g

正常方式如下:

    pStmt = conn.prepareStatement("SELECT DETAILS FROM PROGRAM_HISTORY WHERE id = 12");
    rset = pStmt.executeQuery();
    Clob detailsClob= rset.getClob(1);
    Writer writer = adapter.getCharacterOutputStream(detailsClob);
    writer.write("add more details"); 

    pStmt = conn.prepareStatement("UPDATE PROGRAM_HISTORY SET DETAILS = ? WHERE ID = 12");
    pStmt.setClob(1, detailsClob);
    pStmt.execute();

这有点复杂,我不需要先读取然后更新它。就像更新varchar一样。

1 个答案:

答案 0 :(得分:6)

不确定为什么你称之为“正常”方式,但以下方法适用于我。

在更新之前不需要检索任何数据。

String value = "So long and thanks for all the fish";
StringReader reader = new StringReader(value);
pStmt = conn.prepareStatement("UPDATE PROGRAM_HISTORY SET DETAILS = ? WHERE ID = 12");
pStmt.setCharacterStream(1, reader, value.length());
pStmt.executeUpdate();