科技行者

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

知识库

知识库 安全导航



ZDNet>软件频道>数据库-zhiding>oracle中读写blob字段的问题

  • 扫一扫
    分享文章到微信

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

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

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

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


一是必?取消自?提交。
 

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

查看本文来源