文章詳情頁(yè)
輕松掌握jdbc操縱Oracle數(shù)據(jù)庫(kù)lob字段
瀏覽:16日期:2024-07-07 09:04:38
在Oracle數(shù)據(jù)庫(kù)中,lob(large object,大型對(duì)象)類型的字段使用的頻率越來(lái)越高了。因?yàn)檫@種類型的字段,容量大(最多能容納4gb的數(shù)據(jù)),且一個(gè)表中可以有多個(gè)這種類型的字段,很靈活,適用于數(shù)據(jù)量非常大的業(yè)務(wù)領(lǐng)域(如圖象、檔案等)。而long、long raw等類型的字段,雖然存儲(chǔ)容量也不小(可達(dá)2gb),但由于一個(gè)表中只能有一個(gè)這樣類型的字段的限制,現(xiàn)在已很少使用了。lob類型分為blob和clob兩種:blob即二進(jìn)制大型對(duì)象(binary large object),適用于存貯非文本的字節(jié)流數(shù)據(jù)(如程序、圖象、影音等)。而clob,即字符型大型對(duì)象(character largeobject),則與字符集相關(guān),適于存貯文本型的數(shù)據(jù)(如歷史檔案、大部頭著作等)。下面以程序?qū)嵗f(shuō)明通過(guò)jdbc操縱oracle數(shù)據(jù)庫(kù)lob類型字段。先建立如下兩個(gè)測(cè)試用的數(shù)據(jù)庫(kù)表,power designer pd模型如下:建表sql語(yǔ)句為:create table test_clob ( id number(3), clobcol clob) create table test_blob ( id number(3), blobcol blob)lob對(duì)象的存取1、往數(shù)據(jù)庫(kù)中插入一個(gè)新的clob對(duì)象public static void clobinsert(string infile) throws exception { /* 設(shè)定不自動(dòng)提交 */ boolean defaultcommit = conn.getautocommit(); conn.setautocommit(false); try { /* 插入一個(gè)空的clob對(duì)象 */ stmt.executeupdate('insert into test_clob values (’111’, empty_clob())'); /* 查詢此clob對(duì)象并鎖定 */ resultset rs = stmt.executequery('select clobcol from test_clob where id=’111’ for update'); while (rs.next()) { /* 取出此clob對(duì)象 */ oracle.sql.clob clob = (oracle.sql.clob)rs.getclob('clobcol'); /* 向clob對(duì)象中寫(xiě)入數(shù)據(jù) */ bufferedwriter out = new bufferedwriter(clob.getcharacteroutputstream()); bufferedreader in = new bufferedreader(new filereader(infile)); int c; while ((c=in.read())!=-1) { out.write(c); } in.close(); out.close(); } /* 正式提交 */ conn.commit(); } catch (exception ex) { /* 出錯(cuò)回滾 */ conn.rollback(); throw ex; } /* 恢復(fù)原提交狀態(tài) */ conn.setautocommit(defaultcommit); }2、修改clob對(duì)象(是在原clob對(duì)象基礎(chǔ)上進(jìn)行覆蓋式的修改)public static void clobmodify(string infile) throws exception { /* 設(shè)定不自動(dòng)提交 */ boolean defaultcommit = conn.getautocommit(); conn.setautocommit(false); try { /* 查詢clob對(duì)象并鎖定 */ resultset rs = stmt.executequery('select clobcol from test_clob where id=’111’ for update'); while (rs.next()) { /* 獲取此clob對(duì)象 */ oracle.sql.clob clob = (oracle.sql.clob)rs.getclob('clobcol'); /* 進(jìn)行覆蓋式修改 */ bufferedwriter out = new bufferedwriter(clob.getcharacteroutputstream()); bufferedreader in = new bufferedreader(new filereader(infile)); int c; while ((c=in.read())!=-1) { out.write(c); } in.close(); out.close(); } /* 正式提交 */ conn.commit(); } catch (exception ex) { /* 出錯(cuò)回滾 */ conn.rollback(); throw ex; } /* 恢復(fù)原提交狀態(tài) */ conn.setautocommit(defaultcommit); }
標(biāo)簽:
JDBC
排行榜
