科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道oracle中读写blob字段的问题

oracle中读写blob字段的问题

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

LOB?型分?BLOB和CLOB??:BLOB即二?制大型?像(Binary Large Object),?用於存?非文本的字?流??(如程序、?像、影音等)。而CLOB,即字符型大型?像(Character Large Object)。

作者:中国IT实验室 来源:中国IT实验室 2007年10月11日

关键字: ORACLE

  • 评论
  • 分享微博
  • 分享邮件

在本页阅读全文(共4页)

二、 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);

}


查看本文来源

    • 评论
    • 分享微博
    • 分享邮件