oracle中读写blob字段的问题解析

所属分类: 数据库 / oracle 阅读数: 50
收藏 0 赞 0 分享

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

3、替换CLOB对像(将原CLOB对像清除,换成一个全新的CLOB对像)

复制代码 代码如下:

public static void clobReplace(String infile) throws Exception
{
/* 设定不自动提交 */
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");
/* 更新数据 */
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);
}

4、CLOB对像读取

复制代码 代码如下:

public static void clobRead(String outfile) throws Exception
{
/* 设定不自动提交 */
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;
}

/* 恢复原提交状态 */
conn.setAutoCommit(defaultCommit);
}

二、 BLOB对象的存取

1、 向数据库中插入一个新的BLOB对像

复制代码 代码如下:

public static void blobInsert(String infile) throws Exception
{
/* 设定不自动提交 */
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对像中写入数据 */
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;
}
/* 恢复原提交状态 */
conn.setAutoCommit(defaultCommit);
}

2、修改BLOB对像(是在原BLOB对像基础上进行覆盖式的修改)

复制代码 代码如下:

public static void blobModify(String infile) throws Exception
{
/* 设定不自动提交 */
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对像中写入数据 */
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;
}

/* 恢复原提交状态 */
conn.setAutoCommit(defaultCommit);
}

3、替换BLOB对像(将原BLOB对像清除,换成一个全新的BLOB对像)

复制代码 代码如下:

public static void blobReplace(String infile) throws Exception
{
/* 设定不自动提交 */
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对像中写入数据 */
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;
}

/* 恢复原提交状态 */
conn.setAutoCommit(defaultCommit);
}

4、BLOB对像读取

复制代码 代码如下:

public static void blobRead(String outfile) throws Exception
{
/* 设定不自动提交 */
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");
/* 以二进制形式输出 */
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;
}

/* 恢复原提交状态 */
conn.setAutoCommit(defaultCommit);
}


观察上述程序对LOB类型字段的存取,我们可以看出,较之其它类型字段,有下面几个显著不同的特点:

一是必须取消自动提交。

更多精彩内容其他人还在看

ORACLE中如何找到未提交事务的SQL语句详解

这篇文章主要给大家介绍了关于ORACLE中如何找到未提交事务的SQL语句,文中通过示例代码介绍的非常详细,对大家学习或者使用ORACLE具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

Oracle分组函数之ROLLUP的基本用法

这篇文章主要给大家介绍了关于Oracle分组函数之ROLLUP的基本用法,文中通过示例代码介绍的非常详细,对大家学习或者使用Oracle数据库具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

Oracle表空间设置和管理浅析

这篇文章主要给大家介绍了关于Oracle表空间设置和管理的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Oracle具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

Oracle使用MyBatis中RowBounds实现分页查询功能

这篇文章主要介绍了Oracle使用MyBatis中RowBounds实现分页查询 ,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Oracle自定义脱敏函数的代码详解

这篇文章主要介绍了Oracle自定义脱敏函数的实例代码,代码简单易懂,非常不错,具有一定的参考借鉴价值 ,需要的朋友可以参考下
收藏 0 赞 0 分享

oracle查询截至到当前日期月份所在年份的所有月份

这篇文章主要介绍了oracle查询截至到当前日期月份所在年份的所有月份,本文通过代码给大家介绍的非常详细,具有一定的参考借鉴价值 ,需要的朋友可以参考下
收藏 0 赞 0 分享

oracle数据库导入.dmp脚本的sql 语句

这篇文章主要介绍了oracle数据库导入.dmp脚本的sql语句,非常不错,具有一定的参考借鉴价值 ,需要的朋友可以参考下
收藏 0 赞 0 分享

oracle通过1条语句插入多个值的方法示例

这篇文章主要给大家介绍了关于oracle通过1条语句插入多个值的方法,文中通过示例代码介绍的非常详细,对大家学习或者使用oracle具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

oracle导入导出表时因一个分号引发的惨案

这篇文章主要给大家介绍了关于oracle导入导出表时因一个分号引发的惨案,文中通过示例代码介绍的非常详细,对大家学习或者使用oracle具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

Oracle创建设置查询权限用户的方法

这篇文章主要给大家介绍了关于Oracle创建设置查询权限用户的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Oracle具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享
查看更多