談談在ORACLE下開發JAVA程式的問題

問題一:如保載入jdbc驅動程式:
正常我們載入驅動程式有三個途徑:
1)class.forname(string)這想當於classloader一個string指定的類,在裝載時把該驅動程式的靜態內容都初始化,其實這時驅動程式類調用了drivermanager.registerdriver(driver);方法
2)使用系統屬性:system.getproperty().load(new fileinputstream("屬性檔案"));
在屬性檔案中指定jdbc.driver=drivername 這樣的好處是可以同時載入多個jdbc,換資料庫時不用訪問java原始碼,只是修改屬性檔案
3)直接registerdriver(driver)這種方法最可靠,可以在任何環境下使用。

1)方法簡單,但ms的jvm不能正確初始化。比如使用ie時在applet中就不能使用,應該用3)的方法。但3)方法在靈活性方面不如2),可以根據環境綜合考慮。


問題二:大對象存儲
一般來說,大對象存儲是把檔案存到資料庫中,當然也可以記憶體中的超大字元串。對於象圖片這樣的檔案當然是用二進制存儲,這裡有很多誤區,網路上的教程99%都是行不通的,連sun自己的文檔都一直錯誤,雖然錯誤很小。按說二進制檔案應該存為blob類型,但jbdc2並不能直接對blob存入二進制檔案,如果你這樣做,會得到一個io而不是sql異常,為此花了我近兩個小時才弄清楚。
如果要把一個二制檔案存入oracle,用標準的jdbc你就要用long row類型:
create table tb_file(name varchar(20),detail long row);
然後
file file = new file("aaa.gif");
int filelength =(int) file.length();
inputstream fin = new fileinputstream(file);
preparedstatement pstmt = con.preparestatement("insert into tb_file values(´aaa.gif´,?)");
pstmt.setbinarystream (1, fin, filelength);
pstmt.executeupdate();

如果你一定要用blob存儲,你就必須用oracle自己的方法:
create table tb_file(name varchar(20),detail blob);
con.setautocommit(false);
stmt.executeupdate("insert into tb_file values(´aaa.gif´,empty_blob())");
下面必須select得到blob的對象再向里寫:
rs = stmt.executequery("select detail from tb_file where name=´aaa.gif´ for upfdate" );
if(rs.next())
{
blob blob = rs.getblob(1);
binaryoutputstream out = ((oracle.sql.blob)blob).getbinaryoutputstream();
byte[] b = new byte[((oracle.sql.blob)blob).getbuffersize];
inputstream fin = new fileinputstream(file);
int len = 0;
while( (len = fin.read(b)) != -1)
out.write(b,0,len);
fin.close();
out.close();
con.commit();
}

同樣讀取數據你並不能象long row那樣
inputstream in = rs.getbinaryinputstream("detail");
而要
blob blob = rs.getblob("detail");
in = blob.getbinarystream();

問題三:可滾動結果集
oracle 明確說明不支持結果集滾動,那么我們用jdbc2得到一個可滾動的結果集就是同jdbc自己支持的,就是說結果集要在內在中高度快取,很多很多的開發者都錯誤地認為是資料庫支持的。只是他們沒有真正查詢大量行,如果真的查詢大量行的話肯定是死定了!!!!!!對於超大量行的數據,情願返回到它的笨方法也不要使用可滾動結果集