如何将空字符串更新为oracle Clob

我知道它使用SQL

update activity set REFERENCE = EMPTY_CLOB() where id = ? 

但我不能这样做,我不能在SQL中硬编码’EMPTY_CLOB()’。 我用的方式如下:

 String empty_string = ""; conn = getConnection(); pStmt = conn.prepareStatement("SELECT REFERENCE FROM activity WHERE ID = ? FOR UPDATE"); pStmt.setLong(1, 1); rset = pStmt.executeQuery(); Clob clob = null; while (rset.next()) { clob = rset.getClob(1); Writer writer = adapter.getCharacterOutputStream(clob); writer.write(empty_string); writer.flush(); writer.close(); } pStmt = conn.prepareStatement("update activity set REFERENCE = ? WHERE ID = ?"); pStmt.setClob(1, clob); pStmt.setLong(2, 1); pStmt.executeUpdate(); 

但它没有用。 clob没有更新为空字符串,它仍然存储为以前的值。

任何人都可以帮我这个吗?

正如我在你的另一个问题中已经提到的那样:根据我的经验,getClob()和setClob()无法正常工作。

请改用setCharacterStream()

 StringReader clob = new StringReader(""); pStmt = conn.prepareStatement("update activity set REFERENCE = ? WHERE ID = ?"); pStmt.setCharacterStream(1, clob, 0); pStmt.setLong(2, 1); pStmt.executeUpdate(); 

这样你也可以在更新之前删除不必要的SELECT,这也将提高性能。

另一种选择是简单地将该列设置为NULL

编辑:

对于较新的驱动程序(11.x),您可能还想尝试在CLOB列上使用setString()getString()

只有当您使用在跨越多个语句的事务期间要保留的LOB定位符时才需要锁定行(至少这是我对手册链接引用的理解)。