婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > oracle中讀寫blob字段的問題解析

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

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

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):

一是必須取消自動提交。

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

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

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《oracle中讀寫blob字段的問題解析》,本文關(guān)鍵詞  oracle,中,讀寫,blob,字段,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《oracle中讀寫blob字段的問題解析》相關(guān)的同類信息!
  • 本頁收集關(guān)于oracle中讀寫blob字段的問題解析的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 临桂县| 溆浦县| 宁津县| 纳雍县| 商洛市| 双江| 台前县| 襄城县| 龙口市| 女性| 紫云| 安图县| 繁昌县| 博罗县| 玉屏| 海城市| 达州市| 汾阳市| 大邑县| 蓝山县| 富源县| 秦安县| 祁连县| 托里县| 尼勒克县| 阿克苏市| 苍南县| 锡林浩特市| 龙海市| 尉氏县| 镇平县| 莆田市| 东海县| 博野县| 蓬溪县| 迁西县| 五河县| 都匀市| 新源县| 英超| 铁岭县|