在SQL中,LOB分为两种:BLOB和CLOB。二进制大对象称为BLOB,字符型大对象称为CLOB
1、读LOB:
PreparedStatement stat = conn.prepareStatement("SELECT Cover FROM BookCovers WHERE ISBN=?");
stat.set(1, isbn);
ResultSet result = stat.executeQuery();
if(result.next()){
Blob coverBlob = result.getBlob();
Image coverImage = ImageIO.read(coverBlob.getInputStream());
}
类似的,如果获取了Clob对象,那么就可以通过调用getSubString或getCharacterStream来获取其中的字符数据。
2、写LOB:
Blob coverBlob = connection.createBlob();
int offset = 0;
OutputStream out = coverBlob.setBinaryStream(offset);
ImageIO.write(coverImage, "PNG", "out");
PreparedStatement stat = conn.prepareStatement("INSERT INTO Cover VALUES(?,?)");
stat.set(1, isbn);
stat.set(2,coverBlob);
stat.executeUpdate();
类似的,如果获取了Clob对象,那么就可以通过调用connection.createClob()。