數據庫中提供了兩種字段類型 Blob 和 Clob 用于存儲大型字符串或二進制數據(如圖片)。
Blob 采用單字節存儲,適合保存二進制數據,如圖片文件。
Clob 采用多字節存儲,適合保存大型文本數據。
Oracle中處理BLOB/CLOB字段的方式比較特別,所以需要特別注意下面兩點:
1. 在Oracle JDBC中采用流機制對 BLOB/CLOB 進行讀寫操作,所以要注意不能在批處理中讀寫 BLOB/CLOB字段,否則將出現
Stream type cannot be used in batching 異常。
2. Oracle BLOB/CLOB 字段本身擁有一個游標(cursor),JDBC通過游標對Blob/Clob字段進行操作,在Blob/Clob字段創建之前,無法獲取其游標句柄,會出現
Connection reset by peer: socket write error 異常。
正確的做法是:首先創建一個空 Blob/Clob 字段,再從這個空 Blob/Clob字段獲取游標,例如下面的代碼:
PreparedStatement ps = conn.prepareStatement( " insert into PICTURE(image,resume) values(?,?) " );
// 通過oralce.sql.BLOB/CLOB.empty_lob()構造空Blob/Clob對象
ps.setBlob( 1 ,oracle.sql.BLOB.empty_lob());
ps.setClob( 2 ,oracle.sql.CLOB.empty_lob());
ps.excuteUpdate();
ps.close();
// 再次對讀出Blob/Clob句柄
ps = conn.prepareStatement( " select image,resume from PICTURE where id=? for update " );
ps.setInt( 1 , 100 );
ResultSet rs = ps.executeQuery();
rs.next();
oracle.sql.BLOB imgBlob = (oracle.sql.BLOB)rs.getBlob( 1 );
oracle.sql.CLOB resClob = (oracle.sql.CLOB)rs.getClob( 2 );
// 將二進制數據寫入Blob
FileInputStream inStream = new FileInputStream( " c://image.jpg " );
OutputStream outStream = imgBlob.getBinaryOutputStream();
byte [] buf = new byte [ 10240 ];
int len;
while (len = inStream.read(buf) > 0 ) {
outStream.write(buf, 0 ,len);
}
inStream.close();
outStream.cloese();
// 將字符串寫入Clob
resClob.putString( 1 , " this is a clob " );
// 再將Blob/Clob字段更新到數據庫
ps = conn.prepareStatement( " update PICTURE set image=? and resume=? where id=? " );
ps.setBlob( 1 ,imgBlob);
ps.setClob( 2 ,resClob);
ps.setInt( 3 , 100 );
ps.executeUpdate();
ps.close();