Chinaunix首页 | 论坛 | 博客
  • 博客访问: 669793
  • 博文数量: 759
  • 博客积分: 5000
  • 博客等级: 大校
  • 技术积分: 4845
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-27 13:51
文章分类

全部博文(759)

文章存档

2011年(1)

2008年(758)

我的朋友

分类:

2008-10-27 13:57:16

    LOB類型分為BLOB和CLOB兩種:BLOB即二進制大型對像(Binary Large Object),適用於存貯非文本的字節流數據(如程序、圖像、影音等)。而CLOB,即字符型大型對像(Character Large Object),則與字符集相關,適於存貯文本型的數據(如歷史檔案、大部頭著作等)。


下面以程序實例說明通過JDBC操縱數據庫LOB類型字段的幾種情況。


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

 

建表SQL語句為:

CREATE TABLE TEST_CLOB ( ID NUMBER(3), CLOBCOL CLOB)

CREATE TABLE TEST_BLOB ( ID NUMBER(3), BLOBCOL BLOB)


一、 CLOB對象的存取


1、往數據庫中插入一個新的CLOB對像


public static void clobInsert(String infile) throws Exception

{

/* 設定不自動提交 */

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對像中寫入數據 */

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;

}


/* 恢復原提交狀態 */

conn.setAutoCommit(defaultCommit);

}


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


public static void clobModify(String infile) throws Exception

{

/* 設定不自動提交 */

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");

/* 進行覆蓋式修改 */

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;

}


/* 恢復原提交狀態 */

conn.setAutoCommit(defaultCommit);

}


[1]    

【责编:Peng】

--------------------next---------------------

阅读(395) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~