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

时间:2021-05-02

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

ResultSet rs = stmt.executeQuery("SELECT CLOBCOL FROM TEST_CLOB WHERE 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");
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对像

复制代码 代码如下:

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章