create table images (imgname name,imgoid oid); 要插入一幅圖象,你可以: File file = new File("myimage.gif"); FileInputStream fis = new FileInputStream(file); PreparedStatement ps = conn.prepareStatement("insert into images values (?,?)"); ps.setString(1,file.getName()); ps.setBinaryStream(2,fis,file.length()); ps.executeUpdate(); ps.close(); fis.close(); 現在,在這個例子里,setBinaryStream 從一個流里面把一定字節的數據轉換到大對象里,然后把(大對象的) OID 存儲到引用它的字段里. 檢索一幅圖象甚至更快(我在這里使用 PreparedStatement ,當然用 Statement 也是一樣的):
PreparedStatement ps = con.prepareStatement("select oid from images where name=?"); ps.setString(1,"myimage.gif"); ResultSet rs = ps.executeQuery(); if(rs!=null) { while(rs.next()) { InputStream is = rs.getBinaryInputStream(1); // use the stream in some way here is.close(); } rs.close(); } ps.close(); 這里你可以看到這里大對象是當做一個 InputStream (輸入流)檢索的.你還會注意到我們在處理結果的下一行之前關閉了流.這是 JDBC 規范的一部分,該規范指出任何返回的 InputStream 在調用 ResultSet.next() 或 ResultSet.close() 后都要被關閉.