最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

oracle中讀寫blob字段的問題解析

 更新時間:2013年09月25日 11:45:21   作者:  
這篇文章以程序?qū)嵗f明通過JDBC操縱Oracle數(shù)據(jù)庫LOB類型字段的幾種情況

LOB類型分為BLOB和CLOB兩種:BLOB即二進(jìn)制大型對像(Binary Large Object),適用于存貯非文本的字節(jié)流數(shù)據(jù)(如程序、圖像、影音等)。而CLOB,即字符型大型對像(Character Large Object),則與字符集相關(guān),適于存貯文本型的數(shù)據(jù)(如歷史檔案、大部頭著作等)。
下面以程序?qū)嵗f明通過JDBC操縱Oracle數(shù)據(jù)庫LOB類型字段的幾種情況。

先建立如下兩個測試用的數(shù)據(jù)庫表,Power Designer PD模型如下:

建表SQL語句為:
CREATE TABLE TEST_CLOB ( ID NUMBER(3), CLOBCOL CLOB)
CREATE TABLE TEST_BLOB ( ID NUMBER(3), BLOBCOL BLOB)

一、 CLOB對象的存取

1、往數(shù)據(jù)庫中插入一個新的CLOB對像

復(fù)制代碼 代碼如下:

public static void clobInsert(String infile) throws Exception
{
/* 設(shè)定不自動提交 */
boolean defaultCommit = conn.getAutoCommit();
conn.setAutoCommit(false);

try {
/* 插入一個空的CLOB對像 */
stmt.executeUpdate("INSERT INTO TEST_CLOB VALUES ('111', EMPTY_CLOB())");
/* 查詢此CLOB對象并鎖定 */
ResultSet rs = stmt.executeQuery("SELECT CLOBCOL FROM TEST_CLOB WHERE ID='111' FOR UPDATE");
while (rs.next()) {
/* 取出此CLOB對像 */
oracle.sql.CLOB clob = (oracle.sql.CLOB)rs.getClob("CLOBCOL");
/* 向CLOB對像中寫入數(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) {
/* 出錯回滾 */
conn.rollback();
throw ex;
}

/* 恢復(fù)原提交狀態(tài) */
conn.setAutoCommit(defaultCommit);
}

2、修改CLOB對像(是在原CLOB對像基礎(chǔ)上進(jìn)行覆蓋式的修改)

復(fù)制代碼 代碼如下:

public static void clobModify(String infile) throws Exception
{
/* 設(shè)定不自動提交 */
boolean defaultCommit = conn.getAutoCommit();
conn.setAutoCommit(false);

try {
/* 查詢CLOB對象并鎖定 */
ResultSet rs = stmt.executeQuery("SELECT CLOBCOL FROM TEST_CLOB WHERE ID='111' FOR UPDATE");
while (rs.next()) {
/* 獲取此CLOB對像 */
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) {
/* 出錯回滾 */
conn.rollback();
throw ex;
}

/* 恢復(fù)原提交狀態(tài) */
conn.setAutoCommit(defaultCommit);
}

3、替換CLOB對像(將原CLOB對像清除,換成一個全新的CLOB對像)

復(fù)制代碼 代碼如下:

public static void clobReplace(String infile) throws Exception
{
/* 設(shè)定不自動提交 */
boolean defaultCommit = conn.getAutoCommit();
conn.setAutoCommit(false);

try {
/* 清空原CLOB對像 */
stmt.executeUpdate("UPDATE TEST_CLOB SET CLOBCOL=EMPTY_CLOB() WHERE ID='111'");
/* 查詢CLOB對象并鎖定 */
ResultSet rs = stmt.executeQuery("SELECT CLOBCOL FROM TEST_CLOB WHERE ID='111' FOR UPDATE");
while (rs.next()) {
/* 獲取此CLOB對像 */
oracle.sql.CLOB clob = (oracle.sql.CLOB)rs.getClob("CLOBCOL");
/* 更新數(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) {
/* 出錯回滾 */
conn.rollback();
throw ex;
}

/* 恢復(fù)原提交狀態(tài) */
conn.setAutoCommit(defaultCommit);
}

4、CLOB對像讀取

復(fù)制代碼 代碼如下:

public static void clobRead(String outfile) throws Exception
{
/* 設(shè)定不自動提交 */
boolean defaultCommit = conn.getAutoCommit();
conn.setAutoCommit(false);

try {
/* 查詢CLOB對像 */
ResultSet rs = stmt.executeQuery("SELECT * FROM TEST_CLOB WHERE ID='111'");
while (rs.next()) {
/* 獲取CLOB對像 */
oracle.sql.CLOB clob = (oracle.sql.CLOB)rs.getClob("CLOBCOL");
/* 以字符形式輸出 */
BufferedReader in = new BufferedReader(clob.getCharacterStream());
BufferedWriter out = new BufferedWriter(new FileWriter(outfile));
int c;
while ((c=in.read())!=-1) {
out.write(c);
}
out.close();
in.close();
}
} catch (Exception ex) {
conn.rollback();
throw ex;
}

/* 恢復(fù)原提交狀態(tài) */
conn.setAutoCommit(defaultCommit);
}

二、 BLOB對象的存取

1、 向數(shù)據(jù)庫中插入一個新的BLOB對像

復(fù)制代碼 代碼如下:

public static void blobInsert(String infile) throws Exception
{
/* 設(shè)定不自動提交 */
boolean defaultCommit = conn.getAutoCommit();
conn.setAutoCommit(false);

try {
/* 插入一個空的BLOB對像 */
stmt.executeUpdate("INSERT INTO TEST_BLOB VALUES ('222', EMPTY_BLOB())");
/* 查詢此BLOB對象并鎖定 */
ResultSet rs = stmt.executeQuery("SELECT BLOBCOL FROM TEST_BLOB WHERE ID='222' FOR UPDATE");
while (rs.next()) {
/* 取出此BLOB對像 */
oracle.sql.BLOB blob = (oracle.sql.BLOB)rs.getBlob("BLOBCOL");
/* 向BLOB對像中寫入數(shù)據(jù) */
BufferedOutputStream out = new BufferedOutputStream(blob.getBinaryOutputStream());
BufferedInputStream in = new BufferedInputStream(new FileInputStream(infile));
int c;
while ((c=in.read())!=-1) {
out.write(c);
}
in.close();
out.close();
}
/* 正式提交 */
conn.commit();
} catch (Exception ex) {
/* 出錯回滾 */
conn.rollback();
throw ex;
}
/* 恢復(fù)原提交狀態(tài) */
conn.setAutoCommit(defaultCommit);
}

2、修改BLOB對像(是在原BLOB對像基礎(chǔ)上進(jìn)行覆蓋式的修改)

復(fù)制代碼 代碼如下:

public static void blobModify(String infile) throws Exception
{
/* 設(shè)定不自動提交 */
boolean defaultCommit = conn.getAutoCommit();
conn.setAutoCommit(false);

try {
/* 查詢BLOB對象并鎖定 */
ResultSet rs = stmt.executeQuery("SELECT BLOBCOL FROM TEST_BLOB WHERE ID='222' FOR UPDATE");
while (rs.next()) {
/* 取出此BLOB對像 */
oracle.sql.BLOB blob = (oracle.sql.BLOB)rs.getBlob("BLOBCOL");
/* 向BLOB對像中寫入數(shù)據(jù) */
BufferedOutputStream out = new BufferedOutputStream(blob.getBinaryOutputStream());
BufferedInputStream in = new BufferedInputStream(new FileInputStream(infile));
int c;
while ((c=in.read())!=-1) {
out.write(c);
}
in.close();
out.close();
}
/* 正式提交 */
conn.commit();
} catch (Exception ex) {
/* 出錯回滾 */
conn.rollback();
throw ex;
}

/* 恢復(fù)原提交狀態(tài) */
conn.setAutoCommit(defaultCommit);
}

3、替換BLOB對像(將原BLOB對像清除,換成一個全新的BLOB對像)

復(fù)制代碼 代碼如下:

public static void blobReplace(String infile) throws Exception
{
/* 設(shè)定不自動提交 */
boolean defaultCommit = conn.getAutoCommit();
conn.setAutoCommit(false);

try {
/* 清空原BLOB對像 */
stmt.executeUpdate("UPDATE TEST_BLOB SET BLOBCOL=EMPTY_BLOB() WHERE ID='222'");
/* 查詢此BLOB對象并鎖定 */
ResultSet rs = stmt.executeQuery("SELECT BLOBCOL FROM TEST_BLOB WHERE ID='222' FOR UPDATE");
while (rs.next()) {
/* 取出此BLOB對像 */
oracle.sql.BLOB blob = (oracle.sql.BLOB)rs.getBlob("BLOBCOL");
/* 向BLOB對像中寫入數(shù)據(jù) */
BufferedOutputStream out = new BufferedOutputStream(blob.getBinaryOutputStream());
BufferedInputStream in = new BufferedInputStream(new FileInputStream(infile));
int c;
while ((c=in.read())!=-1) {
out.write(c);
}
in.close();
out.close();
}
/* 正式提交 */
conn.commit();
} catch (Exception ex) {
/* 出錯回滾 */
conn.rollback();
throw ex;
}

/* 恢復(fù)原提交狀態(tài) */
conn.setAutoCommit(defaultCommit);
}

4、BLOB對像讀取

復(fù)制代碼 代碼如下:

public static void blobRead(String outfile) throws Exception
{
/* 設(shè)定不自動提交 */
boolean defaultCommit = conn.getAutoCommit();
conn.setAutoCommit(false);

try {
/* 查詢BLOB對像 */
ResultSet rs = stmt.executeQuery("SELECT BLOBCOL FROM TEST_BLOB WHERE ID='222'");
while (rs.next()) {
/* 取出此BLOB對像 */
oracle.sql.BLOB blob = (oracle.sql.BLOB)rs.getBlob("BLOBCOL");
/* 以二進(jìn)制形式輸出 */
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(outfile));
BufferedInputStream in = new BufferedInputStream(blob.getBinaryStream());
int c;
while ((c=in.read())!=-1) {
out.write(c);
}
in.close();
out.close();
}
/* 正式提交 */
conn.commit();
} catch (Exception ex) {
/* 出錯回滾 */
conn.rollback();
throw ex;
}

/* 恢復(fù)原提交狀態(tài) */
conn.setAutoCommit(defaultCommit);
}


觀察上述程序?qū)OB類型字段的存取,我們可以看出,較之其它類型字段,有下面幾個顯著不同的特點(diǎn):

一是必須取消自動提交。

相關(guān)文章

  • SQL案例學(xué)習(xí)之字符串的合并與拆分方法總結(jié)

    SQL案例學(xué)習(xí)之字符串的合并與拆分方法總結(jié)

    這篇文章主要給大家介紹了關(guān)于SQL案例學(xué)習(xí)之字符串的合并與拆分的相關(guān)資料,文中分別介紹了兩種方法,對大家學(xué)習(xí)或者使用oracle具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2022-08-08
  • plsql和tsql常用函數(shù)比對

    plsql和tsql常用函數(shù)比對

    plsql與tsql的語法不同,大家可以參考下。
    2009-09-09
  • Oracle查詢表占用空間大小方式

    Oracle查詢表占用空間大小方式

    這篇文章主要介紹了Oracle查詢表占用空間大小方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • Oracle Form中COMMIT的概述及使用技巧

    Oracle Form中COMMIT的概述及使用技巧

    針對form上面的數(shù)據(jù)變動提交到后臺數(shù)據(jù)庫,同時數(shù)據(jù)庫提交數(shù)據(jù),接下來將詳細(xì)介紹下Form中COMMIT的使用,感興趣的你可以參考下本文
    2013-03-03
  • Oracle讀取excel數(shù)據(jù)

    Oracle讀取excel數(shù)據(jù)

    本文給大家介紹Oracle讀取excel數(shù)據(jù)的相關(guān)知識,本文介紹的非常詳細(xì),具有參考借鑒價值,感興趣的朋友一起學(xué)習(xí)吧
    2016-03-03
  • window中oracle環(huán)境變量設(shè)置方法分享

    window中oracle環(huán)境變量設(shè)置方法分享

    這篇文章主要介紹了window中oracle環(huán)境變量設(shè)置的方法,需要的朋友可以參考下
    2014-03-03
  • oracle常用函數(shù)匯總(分享)

    oracle常用函數(shù)匯總(分享)

    以下是對oracle中的常用函數(shù)進(jìn)行了匯總介紹,需要的朋友可以過來參考下
    2013-08-08
  • oracle報錯(ORA-00600)問題處理

    oracle報錯(ORA-00600)問題處理

    最近在做一個項目,使用的是Oracle數(shù)據(jù)庫,近兩天不知道怎么回事,告警日志里總是顯示這個錯誤(ORA-00600:internalerrorcode,arguments:[kcblasm_1],[103],[],[],[],[],[],[])度娘了一下,終于找到解決方式,分享給大家
    2014-08-08
  • ORACLE 如何查詢被鎖定表及如何解鎖釋放session

    ORACLE 如何查詢被鎖定表及如何解鎖釋放session

    后臺數(shù)據(jù)庫操作某一個表時發(fā)現(xiàn)一直出于假死狀態(tài),可能是該表被某一用戶鎖定,接下來為你詳細(xì)介紹下查詢被鎖定表及如何解鎖,感興趣的你可以參考下,希望可以幫助到你
    2013-03-03
  • Oracle學(xué)習(xí)筆記(六)

    Oracle學(xué)習(xí)筆記(六)

    最近需要用的oracle,所以大家好好的學(xué)習(xí)下基礎(chǔ)并整理下資料,希望能幫助到需要的朋友。
    2011-12-12

最新評論

达尔| 文安县| 综艺| 泉州市| 古蔺县| 陵川县| 平定县| 文昌市| 马山县| 湾仔区| 禹州市| 砚山县| 嘉祥县| 芒康县| 田林县| 当涂县| 牟定县| 镇坪县| 平陆县| 资溪县| 万州区| 鄂温| 鹤峰县| 凤山县| 灵宝市| 句容市| 肇州县| 曲麻莱县| 清新县| 仙居县| 海南省| 龙岩市| 九江县| 石楼县| 绥阳县| 泗阳县| 吉木乃县| 安龙县| 大洼县| 惠东县| 阜康市|