Oracle中Blob和Clob类型

1、Oracle中Blob和Clob类型的区别
  • BLOB和CLOB都是大字段类型,BLOB是按二进制来存储的,而CLOB是可以直接存储文字的。其实两个是可以互换的的,或者可以直接用LOB字段代替这两个。但是为了更好的管理ORACLE数据库,通常像图片、文件、音乐等信息就用BLOB字段来存储,先将文件转为二进制再存储进去。而像文章或者是较长的文字,就用CLOB存储,这样对以后的查询更新存储等操作都提供很大的方便。
2、Oracle中Blob和Clob类型的区别操作
  • LOB类型分为BLOB和CLOB两种:BLOB即二进制大型对像(Binary Large Object),适用于存贮非文本的字节流数据(如程序、图像、影音等)。而CLOB,即字符型大型对像(Character Large Object),则与字符集相关,适于存贮文本型的数据(如歷史档案、大部头著作等)。

3、下面以程序实例说明通过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对像

代码如下:

  1. public static void clobInsert(String infile) throws Exception
  2. {
  3. /* 设定不自动提交 */
  4. boolean defaultCommit = conn.getAutoCommit();
  5. conn.setAutoCommit(false);
  6. try {
  7. /* 插入一个空的CLOB对像 */
  8. stmt.executeUpdate("INSERT INTO TEST_CLOB VALUES ('111', EMPTY_CLOB())");
  9. /* 查询此CLOB对象并锁定 */
  10. ResultSet rs = stmt.executeQuery("SELECT CLOBCOL FROM TEST_CLOB WHERE ID='111' FOR UPDATE");
  11. while (rs.next()) {
  12. /* 取出此CLOB对像 */
  13. oracle.sql.CLOB clob = (oracle.sql.CLOB)rs.getClob("CLOBCOL");
  14. /* 向CLOB对像中写入数据 */
  15. BufferedWriter out = new BufferedWriter(clob.getCharacterOutputStream());
  16. BufferedReader in = new BufferedReader(new FileReader(infile));
  17. int c;
  18. while ((c=in.read())!=-1) {
  19. out.write(c);
  20. }
  21. in.close();
  22. out.close();
  23. }
  24. /* 正式提交 */
  25. conn.commit();
  26. }
  27. catch (Exception ex) {
  28. /* 出错回滚 */
  29. conn.rollback();

  30. throw ex;

  31. }

  32. /* 恢复原提交状态 */
  33. conn.setAutoCommit(defaultCommit);
  34. }

2、修改CLOB对像(是在原CLOB对像基础上进行覆盖式的修改)

代码如下:
  1. public static void clobModify(String infile) throws Exception
  2. {
  3. /* 设定不自动提交 */
  4. boolean defaultCommit = conn.getAutoCommit();
  5. conn.setAutoCommit(false);
  6. try {
  7. /* 查询CLOB对象并锁定 */
  8. ResultSet rs = stmt.executeQuery("SELECT CLOBCOL FROM TEST_CLOB WHERE ID='111' FOR UPDATE");
  9. while (rs.next()) {
  10. /* 获取此CLOB对像 */
  11. oracle.sql.CLOB clob = (oracle.sql.CLOB)rs.getClob("CLOBCOL");
  12. /* 进行覆盖式修改 */
  13. BufferedWriter out = new BufferedWriter(clob.getCharacterOutputStream());
  14. BufferedReader in = new BufferedReader(new FileReader(infile));
  15. int c;
  16. while ((c=in.read())!=-1) {
  17. out.write(c);
  18. }
  19. in.close();
  20. out.close();
  21. }
  22. /* 正式提交 */
  23. conn.commit();
  24. }
  25. catch (Exception ex) {
  26. /* 出错回滚 */
  27. conn.rollback();
  28. throw ex;
  29. }
  30. /* 恢复原提交状态 */
  31. conn.setAutoCommit(defaultCommit);
  32. }

3、替换CLOB对像(将原CLOB对像清除,换成一个全新的CLOB对像)

代码如下:
  1. public static void clobReplace(String infile) throws Exception
  2. {

  3. /* 设定不自动提交 */
  4. boolean defaultCommit = conn.getAutoCommit();
  5. conn.setAutoCommit(false);
  6. try {

  7. /* 清空原CLOB对像 */
  8. stmt.executeUpdate("UPDATE TEST_CLOB SET CLOBCOL=EMPTY_CLOB() WHERE ID='111'");
  9. /* 查询CLOB对象并锁定 */
  10. ResultSet rs = stmt.executeQuery("SELECT CLOBCOL FROM TEST_CLOB WHERE ID='111' FOR UPDATE");
  11. while (rs.next()) {
  12. /* 获取此CLOB对像 */
  13. oracle.sql.CLOB clob = (oracle.sql.CLOB)rs.getClob("CLOBCOL");
  14. /* 更新数据 */
  15. BufferedWriter out = new BufferedWriter(clob.getCharacterOutputStream());
  16. BufferedReader in = new BufferedReader(new FileReader(infile));
  17. int c;
  18. while ((c=in.read())!=-1)
  19. {
  20. out.write(c);}in.close();out.close();
  21. }
  22. /* 正式提交 */
  23. conn.commit();
  24. }
  25. catch (Exception ex)
  26. {
  27. /* 出错回滚 */
  28. conn.rollback();
  29. throw ex;
  30. }
  31. /* 恢复原提交状态 */
  32. conn.setAutoCommit(defaultCommit);
  33. }

4、CLOB对像读取

代码如下:
  1. public static void clobRead(String outfile) throws Exception
  2. {
  3. /* 设定不自动提交 */
  4. boolean defaultCommit = conn.getAutoCommit();
  5. conn.setAutoCommit(false);
  6. try {
  7. /* 查询CLOB对像 */
  8. ResultSet rs = stmt.executeQuery("SELECT * FROM TEST_CLOB WHERE ID='111'");
  9. while (rs.next()) {
  10. /* 获取CLOB对像 */
  11. oracle.sql.CLOB clob = (oracle.sql.CLOB)rs.getClob("CLOBCOL");
  12. /* 以字符形式输出 */
  13. BufferedReader in = new BufferedReader(clob.getCharacterStream());
  14. BufferedWriter out = new BufferedWriter(new FileWriter(outfile));
  15. int c;
  16. while ((c=in.read())!=-1) {
  17. out.write(c);
  18. }
  19. out.close();
  20. in.close();
  21. }
  22. } catch (Exception ex) {
  23. conn.rollback();
  24. throw ex;
  25. }
  26. /*回复元提交状态

  27. conn.setAutoCommit(defaultCommit);

  28. }

二、 BLOB对象的存取

1、 向数据库中插入一个新的BLOB对像

代码如下:
  1. public static void blobInsert(String infile) throws Exception
  2. {
  3. /* 设定不自动提交 */
  4. boolean defaultCommit = conn.getAutoCommit();
  5. conn.setAutoCommit(false);
  6. try {
  7. /* 插入一个空的BLOB对像 */
  8. stmt.executeUpdate("INSERT INTO TEST_BLOB VALUES ('222', EMPTY_BLOB())");
  9. /* 查询此BLOB对象并锁定 */
  10. ResultSet rs = stmt.executeQuery("SELECT BLOBCOL FROM TEST_BLOB WHERE ID='222' FOR UPDATE");
  11. while (rs.next()) {
  12. /* 取出此BLOB对像 */
  13. oracle.sql.BLOB blob = (oracle.sql.BLOB)rs.getBlob("BLOBCOL");
  14. /* 向BLOB对像中写入数据 */
  15. BufferedOutputStream out = new BufferedOutputStream(blob.getBinaryOutputStream());
  16. BufferedInputStream in = new BufferedInputStream(new FileInputStream(infile));
  17. int c;
  18. while ((c=in.read())!=-1) {
  19. out.write(c);
  20. }
  21. in.close();
  22. out.close();
  23. }
  24. /* 正式提交 */
  25. conn.commit();
  26. }
  27. catch (Exception ex) {
  28. /* 出错回滚 */
  29. conn.rollback();
  30. throw ex;
  31. }
  32. /* 恢复原提交状态 */
  33. conn.setAutoCommit(defaultCommit);
  34. }

2、修改BLOB对像(是在原BLOB对像基础上进行覆盖式的修改)

 代码如下:
  1. public static void blobModify(String infile) throws Exception
  2. {
  3. /* 设定不自动提交 */
  4. boolean defaultCommit = conn.getAutoCommit();
  5. conn.setAutoCommit(false);
  6. try {
  7. /* 查询BLOB对象并锁定 */
  8. ResultSet rs = stmt.executeQuery("SELECT BLOBCOL FROM TEST_BLOB WHERE ID='222' FOR UPDATE");
  9. while (rs.next()) {
  10. /* 取出此BLOB对像 */
  11. oracle.sql.BLOB blob = (oracle.sql.BLOB)rs.getBlob("BLOBCOL");
  12. /* 向BLOB对像中写入数据 */
  13. BufferedOutputStream out = new BufferedOutputStream(blob.getBinaryOutputStream());
  14. BufferedInputStream in = new BufferedInputStream(new FileInputStream(infile));
  15. int c;
  16. while ((c=in.read())!=-1) {
  17. out.write(c);
  18. }
  19. in.close();
  20. out.close();
  21. }
  22. /* 正式提交 */
  23. conn.commit();
  24. } catch (Exception ex) {
  25. /* 出错回滚 */
  26. conn.rollback();
  27. throw ex;
  28. }
  29. /* 恢复原提交状态 */

  30. conn.setAutoCommit(defaultCommit);

  31. }

3、替换BLOB对像(将原BLOB对像清除,换成一个全新的BLOB对像)

代码如下:

  1. public static void blobReplace(String infile) throws Exception
  2. {
  3. /* 设定不自动提交 */
  4. boolean defaultCommit = conn.getAutoCommit();
  5. conn.setAutoCommit(false);
  6. try {
  7. /* 清空原BLOB对像 */
  8. stmt.executeUpdate("UPDATE TEST_BLOB SET BLOBCOL=EMPTY_BLOB() WHERE ID='222'");
  9. /* 查询此BLOB对象并锁定 */
  10. ResultSet rs = stmt.executeQuery("SELECT BLOBCOL FROM TEST_BLOB WHERE ID='222' FOR UPDATE");
  11. while (rs.next()) {
  12. /* 取出此BLOB对像 */
  13. oracle.sql.BLOB blob = (oracle.sql.BLOB)rs.getBlob("BLOBCOL");
  14. /* 向BLOB对像中写入数据 */
  15. BufferedOutputStream out = new BufferedOutputStream(blob.getBinaryOutputStream());
  16. BufferedInputStream in = new BufferedInputStream(new FileInputStream(infile));
  17. int c;
  18. while ((c=in.read())!=-1) {
  19. out.write(c);
  20. }
  21. in.close();
  22. out.close();
  23. }
  24. /* 正式提交 */
  25. conn.commit();
  26. } catch (Exception ex) {
  27. /* 出错回滚 */
  28. conn.rollback();
  29. throw ex;
  30. }
  31. /* 恢复原提交状态 */

  32. conn.setAutoCommit(defaultCommit);

  33. }

4、BLOB对像读取

代码如下:
  1. public static void blobRead(String outfile) throws Exception
  2. {
  3. /* 设定不自动提交 */
  4. boolean defaultCommit = conn.getAutoCommit();
  5. conn.setAutoCommit(false);
  6. try {
  7. /* 查询BLOB对像 */
  8. ResultSet rs = stmt.executeQuery("SELECT BLOBCOL FROM TEST_BLOB WHERE ID='222'");
  9. while (rs.next()) {
  10. /* 取出此BLOB对像 */
  11. oracle.sql.BLOB blob = (oracle.sql.BLOB)rs.getBlob("BLOBCOL");
  12. /* 以二进制形式输出 */
  13. BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(outfile));
  14. BufferedInputStream in = new BufferedInputStream(blob.getBinaryStream());
  15. int c;
  16. while ((c=in.read())!=-1) {
  17. out.write(c);
  18. }
  19. in.close();
  20. out.close();
  21. }
  22. /* 正式提交 */
  23. conn.commit();
  24. } catch (Exception ex) {
  25. /* 出错回滚 */
  26. conn.rollback();
  27. throw ex;
  28. }
  29. /* 恢复原提交状态 */

  30. conn.setAutoCommit(defaultCommit);

  31. }

观察上述程序对LOB类型字段的存取,我们可以看出,较之其它类型字段,有下面几个显著不同的特点:必须取消自动提交

Oracle中Blob和Clob类型的区别与操作的更多相关文章

  1. 问题:oracle CLOB类型;结果:oracle中Blob和Clob类型的区别

    BLOB和CLOB都是大字段类型,BLOB是按二进制来存储的,而CLOB是可以直接存储文字的.其实两个是可以互换的的,或者可以直接用LOB字段代替这两个.但是为了更好的管理ORACLE数据库,通常像图 ...

  2. oracle中Blob和Clob类型的区别

    BLOB和CLOB都是大字段类型,BLOB是按二进制来存储的,而CLOB是可以直接存储文字的.其实两个是可以互换的的,或者可以直接用LOB字段代替这两个.但是为了更好的管理ORACLE数据库,通常像图 ...

  3. oracle中Blob、Clob、Varchar之间的互相转换

    以下是oracle中Blob.Clob.Varchar之间的互相转换(都是百度找的,亲测可用) Blob转Varchar2: CREATE OR REPLACE FUNCTION blob_to_va ...

  4. Oracle中Blob和Clob

    http://www.cnblogs.com/ztf2008/archive/2009/05/16/1458432.html Blob是指二进制大对象也就是英文Binary Large Object的 ...

  5. Oracle中如何查询CLOB字段类型的内容

    注:本文来源于:<Oracle中如何查询CLOB字段类型的内容> 语法 select * from table_name where dbms_lob.instr(字段名(clod类型), ...

  6. Hibernate保存Blob和Clob类型的数据

    虽然非常不建议在数据库中保存Blob和Clob类型的数据,但真的要有这样的需求呢?这里记录一下使用Hibernate如何向数据库中保存Blob和Clob数据. Oracle和MySql在Blob类型上 ...

  7. Oracle中Union与Union All的区别(适用多个数据库)

    Oracle中Union与Union All的区别(适用多个数据库) 如果我们需要将两个select语句的结果作为一个整体显示出来,我们就需要用到union或者union all关键字.union(或 ...

  8. Hibernate or JPA Annotation中BLOB、CLOB注解写法

    BLOB和CLOB都是大字段类型,BLOB是按二进制字节码来存储的,而CLOB是可以直接存储字符串的. 在hibernate or JPA Annotation中,实体BLOB.CLOB类型的注解与普 ...

  9. Oracle中执行存储过程call和exec区别

    Oracle中执行存储过程call和exec区别 在sqlplus中这两种方法都可以使用: exec pro_name(参数1..); call pro_name(参数1..); 区别: 1. 但是e ...

随机推荐

  1. jQuery学习-----(一)JQuery的'$'符号用法

    1.jQuery的三种$() 1)$()可以是$(expresion),即css选择器.Xpath或html元素,也就是通过上述表达式来匹配目标元素.   比如:$("a")构造的 ...

  2. manifest save for self

    一.使用html5的缓存机制 1.先上规则代码:m.manifest CACHE MANIFEST # 2015-04-24 14:20 #直接缓存的文件 CACHE: /templates/spec ...

  3. 离线文档(DocSet)下载地址汇总

    我分享的百度网盘地址,官网下载慢:http://pan.baidu.com/s/1uOBYQ 名称 下载地址 更新时间 IOS 9.2 031-43202-A.dmg 20151209 OS X 10 ...

  4. PLSQL往Oracle数据库插入中文后变为问号 和 启动PLSQL时提示NLS_LANG在客户端不能确定的解决办法

    PLSQL往Oracle数据库插入中文后变为问号 和 启动PLSQL时提示NLS_LANG在客户端不能确定的解决办法 1.检查服务器的字符编码 Select * from V$NLS_PARAMETE ...

  5. discuz random函数

    在研究邮箱非必填的过程中发现了个比较好用的random函数,在function_core.php中找到声明: function random($length, $numeric = 0) { $see ...

  6. phpcms v9 自定义分页 带下拉跳转

    <?php function new_pages($num, $curr_page, $perpage = 20, $urlrule = '', $array = array(),$setpag ...

  7. 在MVVMLight框架的ViewModel中实现NavigationService

    网上已经有很多方法了,比如通过Messenger来实现等等.这里我只讲述一种我比较喜欢的方法,因为它很方便 首先定义一个ViewModel基类,将所有ViewModel子类继承这个基类.在基类中定义 ...

  8. fedora 禁止nouveau加载

    To remove / disable nouveau drivers from kernel initramfs ## Backup old initramfs nouveau image ## m ...

  9. 为cocos2d-x实现安卓输入框。非全屏,无dialog,绑定到lua

    cocos2d-x官方自带的输入框,简直惨不忍睹,在ios还好,在安卓简直了..用过的都知道... 所以为了用户体验,我们自己搞一个吧.输入框这种东西比较特殊,不像按钮.列表框之类的很容易实现,因为涉 ...

  10. TCP报头

    源端口和目的端口: 各占16位 ,服务相对应的源端口和目的端口. 序列号: 占32位,它的范围在[0~2^32-1],序号随着通信的进行不断的递增,当达到最大值的时候重新回到0在开始递增.TCP是面向 ...