打印本文 打印本文 关闭窗口 关闭窗口
通过JDBC操纵Oracle数据库LOB字段的几种情况分析
作者:武汉SEO闵涛  文章来源:敏韬网  点击数2720  更新时间:2009/4/22 22:08:16  文章录入:mintao  责任编辑:mintao
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) {

/* 出错回滚 */

上一页  [1] [2] [3]  下一页

打印本文 打印本文 关闭窗口 关闭窗口