JDBC中BLOB/long raw字段的读写

来源:互联网 发布:nginx shtml 编辑:程序博客网 时间:2024/05/17 23:45
一、读long raw字段
byte[] bytes = SourceResultSet.getBytes("图像数据"
二、写long raw字段
preparedStmt.setBinaryStream(i, new ByteArrayInputStream(bytes),size);
三、读BLOB字段
oracle.sql.BLOB blob = (oracle.sql.BLOB)resultSet.getBlob(i);
if (blob!=null){
     InputStream is 
= blob.getBinaryStream();
     
byte[] bytes = new byte[BUF_SIZE];  //BUF_SIZE应该大于该字段最大的长度
     
int totalSize = is.read(bytes);       //总共读入的字节数
     is.close();
}

四、写BLOB字段 

如果要同时写入多个字段,先把BLOB字段设为 empty_blob()

从下面的结果集中得到要更新的BLOB字段:targetBlob
select 图像数据 from targetTable where id = 100 for update
oracle.sql.BLOB targetBlob = (oracle.sql.BLOB)TargetResultSet.getBlob("图像数据");

把字节数组写入,关闭输出流,setBlob设置BLOB字段

OutputStream out = targetBlob.getBinaryOutputStream();
out.write(bytes);
out.close();
updateBlobStmt.setBlob(
1, targetBlob);
return updateBlobStmt.executeUpdate();