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

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

一是必须取消自动提交。

(0)
上一篇 2022年3月22日
下一篇 2022年3月22日

相关推荐