主頁(yè) > 知識(shí)庫(kù) > oracle中讀寫(xiě)blob字段的問(wèn)題解析

oracle中讀寫(xiě)blob字段的問(wèn)題解析

熱門(mén)標(biāo)簽:征服者火車(chē)站地圖標(biāo)注 word地圖標(biāo)注方向 美圖秀秀地圖標(biāo)注 征服眼公司地圖標(biāo)注 開(kāi)封智能外呼系統(tǒng)廠(chǎng)家 人工智能地圖標(biāo)注自己能做嗎 外呼線(xiàn)路外顯本地號(hào)碼 百度地圖標(biāo)注素材 阿爾巴尼亞地圖標(biāo)注app

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

先建立如下兩個(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)

一、 CLOB對(duì)象的存取

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

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

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())");
/* 查詢(xún)此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)行覆蓋式的修改)

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

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

try {
/* 查詢(xún)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);
}

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

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

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

try {
/* 清空原CLOB對(duì)像 */
stmt.executeUpdate("UPDATE TEST_CLOB SET CLOBCOL=EMPTY_CLOB() WHERE ID='111'");
/* 查詢(xún)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");
/* 更新數(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);
}

4、CLOB對(duì)像讀取

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

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

try {
/* 查詢(xún)CLOB對(duì)像 */
ResultSet rs = stmt.executeQuery("SELECT * FROM TEST_CLOB WHERE ID='111'");
while (rs.next()) {
/* 獲取CLOB對(duì)像 */
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對(duì)象的存取

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

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

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

try {
/* 插入一個(gè)空的BLOB對(duì)像 */
stmt.executeUpdate("INSERT INTO TEST_BLOB VALUES ('222', EMPTY_BLOB())");
/* 查詢(xún)此BLOB對(duì)象并鎖定 */
ResultSet rs = stmt.executeQuery("SELECT BLOBCOL FROM TEST_BLOB WHERE ID='222' FOR UPDATE");
while (rs.next()) {
/* 取出此BLOB對(duì)像 */
oracle.sql.BLOB blob = (oracle.sql.BLOB)rs.getBlob("BLOBCOL");
/* 向BLOB對(duì)像中寫(xiě)入數(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) {
/* 出錯(cuò)回滾 */
conn.rollback();
throw ex;
}
/* 恢復(fù)原提交狀態(tài) */
conn.setAutoCommit(defaultCommit);
}

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

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

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

try {
/* 查詢(xún)BLOB對(duì)象并鎖定 */
ResultSet rs = stmt.executeQuery("SELECT BLOBCOL FROM TEST_BLOB WHERE ID='222' FOR UPDATE");
while (rs.next()) {
/* 取出此BLOB對(duì)像 */
oracle.sql.BLOB blob = (oracle.sql.BLOB)rs.getBlob("BLOBCOL");
/* 向BLOB對(duì)像中寫(xiě)入數(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) {
/* 出錯(cuò)回滾 */
conn.rollback();
throw ex;
}

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

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

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

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

try {
/* 清空原BLOB對(duì)像 */
stmt.executeUpdate("UPDATE TEST_BLOB SET BLOBCOL=EMPTY_BLOB() WHERE ID='222'");
/* 查詢(xún)此BLOB對(duì)象并鎖定 */
ResultSet rs = stmt.executeQuery("SELECT BLOBCOL FROM TEST_BLOB WHERE ID='222' FOR UPDATE");
while (rs.next()) {
/* 取出此BLOB對(duì)像 */
oracle.sql.BLOB blob = (oracle.sql.BLOB)rs.getBlob("BLOBCOL");
/* 向BLOB對(duì)像中寫(xiě)入數(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) {
/* 出錯(cuò)回滾 */
conn.rollback();
throw ex;
}

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

4、BLOB對(duì)像讀取

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

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

try {
/* 查詢(xún)BLOB對(duì)像 */
ResultSet rs = stmt.executeQuery("SELECT BLOBCOL FROM TEST_BLOB WHERE ID='222'");
while (rs.next()) {
/* 取出此BLOB對(duì)像 */
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) {
/* 出錯(cuò)回滾 */
conn.rollback();
throw ex;
}

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


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

一是必須取消自動(dòng)提交。

您可能感興趣的文章:
  • oracle刪除表字段和oracle表增加字段
  • oracle使用sql語(yǔ)句增加字段示例(sql刪除字段語(yǔ)句)
  • 簡(jiǎn)單三步輕松實(shí)現(xiàn)ORACLE字段自增
  • 實(shí)現(xiàn)oracle數(shù)據(jù)庫(kù)字段自增長(zhǎng)(兩種方式)
  • Oracle數(shù)據(jù)庫(kù)表中字段順序的修改方法

標(biāo)簽:泰安 海北 宜春 酒泉 六安 葫蘆島 淮南 孝感

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《oracle中讀寫(xiě)blob字段的問(wèn)題解析》,本文關(guān)鍵詞  oracle,中,讀寫(xiě),blob,字段,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《oracle中讀寫(xiě)blob字段的問(wèn)題解析》相關(guān)的同類(lèi)信息!
  • 本頁(yè)收集關(guān)于oracle中讀寫(xiě)blob字段的問(wèn)題解析的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章