科技行者

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

知识库

知识库 安全导航

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

oracle中读写blob字段的问题

  • 扫一扫
    分享文章到微信

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

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

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

关键字: ORACLE

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

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

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?型字段的存取,我?可以看出,?之其它?型字段,有下面???著不同的特?:


一是必?取消自?提交。
 

Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1475385

查看本文来源

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