文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

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

2022-11-15 23:46

关注

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 {

stmt.executeUpdate("INSERT INTO TEST_CLOB VALUES ('111', EMPTY_CLOB())");

ResultSet rs = stmt.executeQuery("SELECT CLOBCOL FROM TEST_CLOB WHERE ID='111' FOR UPDATE");
while (rs.next()) {

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

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

复制代码 代码如下:

public static void clobModify(String infile) throws Exception
{

boolean defaultCommit = conn.getAutoCommit();
conn.setAutoCommit(false);

try {

ResultSet rs = stmt.executeQuery("SELECT CLOBCOL FROM TEST_CLOB WHERE ID='111' FOR UPDATE");
while (rs.next()) {

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 {

stmt.executeUpdate("UPDATE TEST_CLOB SET CLOBCOL=EMPTY_CLOB() WHERE ID='111'");

ResultSet rs = stmt.executeQuery("SELECT CLOBCOL FROM TEST_CLOB WHERE ID='111' FOR UPDATE");
while (rs.next()) {

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 {

ResultSet rs = stmt.executeQuery("SELECT * FROM TEST_CLOB WHERE ID='111'");
while (rs.next()) {

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 {

stmt.executeUpdate("INSERT INTO TEST_BLOB VALUES ('222', EMPTY_BLOB())");

ResultSet rs = stmt.executeQuery("SELECT BLOBCOL FROM TEST_BLOB WHERE ID='222' FOR UPDATE");
while (rs.next()) {

oracle.sql.BLOB blob = (oracle.sql.BLOB)rs.getBlob("BLOBCOL");

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 {

ResultSet rs = stmt.executeQuery("SELECT BLOBCOL FROM TEST_BLOB WHERE ID='222' FOR UPDATE");
while (rs.next()) {

oracle.sql.BLOB blob = (oracle.sql.BLOB)rs.getBlob("BLOBCOL");

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 {

stmt.executeUpdate("UPDATE TEST_BLOB SET BLOBCOL=EMPTY_BLOB() WHERE ID='222'");

ResultSet rs = stmt.executeQuery("SELECT BLOBCOL FROM TEST_BLOB WHERE ID='222' FOR UPDATE");
while (rs.next()) {

oracle.sql.BLOB blob = (oracle.sql.BLOB)rs.getBlob("BLOBCOL");

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 {

ResultSet rs = stmt.executeQuery("SELECT BLOBCOL FROM TEST_BLOB WHERE ID='222'");
while (rs.next()) {

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类型字段的存取,我们可以看出,较之其它类型字段,有下面几个显著不同的特点:

一是必须取消自动提交。

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯