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(); 現(xiàn)在,在這個(gè)例子里,setBinaryStream 從一個(gè)流里面把一定字節(jié)的數(shù)據(jù)轉(zhuǎn)換到大對(duì)象里,然后把(大對(duì)象的) OID 存儲(chǔ)到引用它的字段里. 檢索一幅圖象甚至更快(我在這里使用 PreparedStatement ,當(dāng)然用 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(); 這里你可以看到這里大對(duì)象是當(dāng)做一個(gè) InputStream (輸入流)檢索的.你還會(huì)注意到我們?cè)谔幚斫Y(jié)果的下一行之前關(guān)閉了流.這是 JDBC 規(guī)范的一部分,該規(guī)范指出任何返回的 InputStream 在調(diào)用 ResultSet.next() 或 ResultSet.close() 后都要被關(guān)閉.
用戶代碼應(yīng)該使用 addFunctions 方法, 因?yàn)檫@個(gè)方法基于一個(gè)查詢,而不是難寫的 oid 代碼. 我們不保證一個(gè)函數(shù)的 oid 是靜態(tài)的, 甚至運(yùn)行在不同服務(wù)器的同版本的數(shù)據(jù)庫也不能保證是統(tǒng)一的.
參數(shù): name - 函數(shù)名 fnid - 函數(shù) id
public void addFunctions(ResultSet rs) throws SQLException