LOB类型分为BLOB和CLOB两种:BLOB即二进制大型对象(Binary Large Object),适用于存贮非文本的字节流数据(如程序、图象、影音等)。而CLOB,即字符型大型对象(Character Large Object),则与字符集相关,适于存贮文本型的数据(如历史档案、大部头著作等)。
下面以程序实例说明通过JDBC操纵Oracle数据库LOB类型字段的几种情况。
先建立如下两个测试用的数据库表,Power Designer PD模型如下:
[img]http://www.csdn.net/Develop/ArticleImages/18/155/CSDN_Dev_Image_2003-6-111028171.jpg[/img] 建表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); }
因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- dcrkj.com 版权所有 赣ICP备2024042791号-2
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务