###數據庫中提供了兩種字段類型 Blob 和 Clob 用於存儲大型字符串或二進制數據java
###Oracle中處理BLOB/CLOB字段的方式比較特別,因此須要特別注意下面兩點:sql
正確的作法是:首先建立一個空 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 " ); //db save over //此時已經完成操做。下面步驟不用也能夠! // 再將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();