根据自己的经验,以及查阅了Oracle的官方文档,都是使用如下处理方法:
1.新建记录,插入BLOB数据
1.1首先新建记录的时候,使用oracle的函数插入一个空的BLOB,假设字段A是BLOB类型的:
insert xxxtable(A,B,C) values(empty_blob(),'xxx','yyyy')
1.2后面再查询刚才插入的记录,然后更新BLOB,在查询前,注意设置Connection的一个属性:
conn.setAutoCommit(false);如果缺少这一步,可能导致fetch out of sequence等异常.
1.3 查询刚才插入的记录,后面要加“ for update ”,如下:
select A from xxxtable where xxx=999 for update ,如果缺少for update,可能出现row containing the LOB value is not locked
的异常
1.4 从查询到的 BLOB字段中,获取blob并进行更新,代码如下:
BLOB blob = (BLOB) rs.getBlob("A");
OutputStream os = blob.getBinaryOutputStream();
BufferedOutputStream output = new BufferedOutputStream(os);