Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1100339
  • 博文数量: 286
  • 博客积分: 3124
  • 博客等级: 中校
  • 技术积分: 5186
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-24 23:42
个人简介

Bomi

文章存档

2015年(1)

2013年(1)

2012年(281)

2008年(3)

分类: 数据库开发技术

2012-02-05 10:23:16

LOB類型分為BLOB和CLOB兩種:BLOB即二進制大型對像(Binary Large Object),適用於存貯非文本的字節流數據(如程序、圖像、影音等)。而CLOB,即字符型大型對像(Character Large Object),則與字符集相關,適於存貯文本型的數據(如歷史檔案、大部頭著作等)。
下面以程序實例說明通過JDBC操縱Oracle數據庫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 ut = 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 ut = 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);
}

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