Oracle中Blob和Clob类型的区别与操作
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对像
代码如下:
- 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类型字段的存取,我们可以看出,较之其它类型字段,有下面几个显著不同的特点:必须取消自动提交
Oracle中Blob和Clob类型的区别与操作的更多相关文章
- 问题:oracle CLOB类型;结果:oracle中Blob和Clob类型的区别
BLOB和CLOB都是大字段类型,BLOB是按二进制来存储的,而CLOB是可以直接存储文字的.其实两个是可以互换的的,或者可以直接用LOB字段代替这两个.但是为了更好的管理ORACLE数据库,通常像图 ...
- oracle中Blob和Clob类型的区别
BLOB和CLOB都是大字段类型,BLOB是按二进制来存储的,而CLOB是可以直接存储文字的.其实两个是可以互换的的,或者可以直接用LOB字段代替这两个.但是为了更好的管理ORACLE数据库,通常像图 ...
- oracle中Blob、Clob、Varchar之间的互相转换
以下是oracle中Blob.Clob.Varchar之间的互相转换(都是百度找的,亲测可用) Blob转Varchar2: CREATE OR REPLACE FUNCTION blob_to_va ...
- Oracle中Blob和Clob
http://www.cnblogs.com/ztf2008/archive/2009/05/16/1458432.html Blob是指二进制大对象也就是英文Binary Large Object的 ...
- Oracle中如何查询CLOB字段类型的内容
注:本文来源于:<Oracle中如何查询CLOB字段类型的内容> 语法 select * from table_name where dbms_lob.instr(字段名(clod类型), ...
- Hibernate保存Blob和Clob类型的数据
虽然非常不建议在数据库中保存Blob和Clob类型的数据,但真的要有这样的需求呢?这里记录一下使用Hibernate如何向数据库中保存Blob和Clob数据. Oracle和MySql在Blob类型上 ...
- Oracle中Union与Union All的区别(适用多个数据库)
Oracle中Union与Union All的区别(适用多个数据库) 如果我们需要将两个select语句的结果作为一个整体显示出来,我们就需要用到union或者union all关键字.union(或 ...
- Hibernate or JPA Annotation中BLOB、CLOB注解写法
BLOB和CLOB都是大字段类型,BLOB是按二进制字节码来存储的,而CLOB是可以直接存储字符串的. 在hibernate or JPA Annotation中,实体BLOB.CLOB类型的注解与普 ...
- Oracle中执行存储过程call和exec区别
Oracle中执行存储过程call和exec区别 在sqlplus中这两种方法都可以使用: exec pro_name(参数1..); call pro_name(参数1..); 区别: 1. 但是e ...
随机推荐
- Qt5中使用lambda表达式
c11新特性中加入了lambda表达式,所以Qt 也支持 需在.pro文件中加入 CONFIG += c++11 例子: QString program = "C:/Windows/Syst ...
- Git 安装与使用(二)
一.分支管理 在Git里,master是主分支,同时可以创建其他分支,支持各分支合并到主分支上,基本命令如下 1.创建分支 git checkout -b dev 创建dev分支,并切换到 ...
- Visual Studio 2015和.Net 2015 预览版在线安装和ISO镜像安装光盘下载
微软刚刚宣布了 Visual Studio 2015和.Net 2015 预览版,并同时提供了下载. 微软在纽约正进行中的#Connect# 全球开发者在线大会上宣布了Visual Studio 20 ...
- javascript的setTimeout以及setInterval休眠问题。
前端码农们在做项目中时候,必定不可少的需要做到轮播效果.但是有些特殊的需求,比如: 需要做到第一个容器内容轮播滚动之后,第二个容器内部再轮播滚动,再第三个容器内容轮播滚动. 这时候我的一开始的思路是: ...
- CentOS6.5 MySQL 配置设置总结笔记
三.登录MySQL 登录MySQL的命令是mysql, mysql 的使用语法如下: mysql [-u username] [-h host] [-p[password]] [dbname] u ...
- Discuz x2.5 单页制作的教程
首先,单页包括该单页的php文件和该单页的模板(.htm)文件,比如:host.php.host.htm 单页的php文件内容如下: <?php require './source/class/ ...
- 考研路之C语言
今天在学习C的时候,又学到了一些新的内容,所以赶紧记录下来. case 1: #include <stdio.h> union exa{ struct{ int a; int b; }ou ...
- Linux多进行之fork
#include <unistd.h> //定义该函数 #include <sys/types.h> //定义函数的返回类型pid_t /* 功能:复制进程 参数:无 返回值: ...
- EXTJS 3.0 资料 控件之 GridPanel属性与方法大全
1.Ext.grid.GridPanel 主要配置项: store:表格的数据集 columns:表格列模式的配置数组,可自动创建ColumnModel列模式 autoExpandColumn:自动充 ...
- Who needs an architect?---Martin Fowler
英文及译文下载链接:http://pan.baidu.com/share/link?shareid=163291504&uk=1428554614 1.文章主题总结 首先我们从文章的几个小标题 ...