oracle数据库中如何处理clob字段方法介绍
时间:2024-01-17 05:55:41
在知识库的建立的时候,用普通VARCHAR2存放文章是显然不够的,只有区区4000的字节,放不了多少字,
而CLOB数据类型,则能最多存放8G的数据。但是这个字段处理起来有比较多的特殊性,记录一下。
插入:
直接写在SQL里面是不行的,一来SQL脚本有字符数限制,而来文章内容包含许多特殊字符,如换行,引号,
之类的东西,很麻烦。网上流行通用做法是先插入一个空CLOB字段,用empty_clob()方法来创建空字段,如:
INSERT INTO T_TOPIC(TOPIC_ID,TOPIC_CONTENT) VALUES(‘0000001',empty_clob());
然后再用SELECT TOPIC_CONTENT FROM T_TOPIC WHERE TOPIC_ID='0000001'FOR UPDATE的查询语句,
来构造一个更新的STATEMENT,在获取到ResultSet之后,对CLOB字段进行更新。
ResultSet rs = pstm.executeQuery();
if(rs.next()){
oracle.sql.CLOB lob =(CLOB)rs.getClob(1);
try {
Writer os = lob.getCharacterOutputStream();
os.write(dr.getField("FLD_CONTENT").asString());
os.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
这个插入和更新操作要放在事务中,即获取到Connection后要设置setAutoCommit(false);
更新:
更新的时候也是采用SEELCT … FOR UPDATE方式
也要设置事务
读取:
CLOB clob = (CLOB)rs.getClob("FLD_CONTENT");
Reader reader = clob.getCharacterStream();
StringBuffer sb=new StringBuffer();
char[] cb = new char[1024];
try {
for(int len = reader.read(cb);len>0;len= reader.read(cb)){
sb.append(cb,0,len);
}
} catch (IOException e) {
throw new SQLException("读取文章内容失败.",e);
}
查询的特殊性:
有CLOB字段的数据表,在SQL语句中不能使用DISTINCT关键字进行筛选,即便关键字不用在CLOB字段名前,
实际上DISTINCT关键字都是对于SQL中所有字段有效。而CLOB字段是不能进行如同LIKE类似的匹配的,所以,
不能进行去重复操作。
两种解决办法:
1、在SQL中调用方法转成VARCHAR2字段后,再DISTINCT,这种方式的局限显而易见。
2、改变SQL脚本的书写方式,先查出没有CLOB字段的集合,然后在在外层用EXISTS关键字或IN关键字进行筛选。
//String sqlsel2 = "select jsonbody from db_ps_listcatalog where" +
// " listtype ='sh11' for update";
// String col="jsonbody";
public boolean updateClob(String sql,String col,String buf){
boolean flag=false;
Statement stem=null;
Connection conn=null;
ResultSet rs=null;
Writer wr = null;
try{
conn= dp.getConnection();
conn.setAutoCommit(false);
stem=conn.createStatement();
rs = stem.executeQuery(sql);
if (rs.next()) {
CLOB clob = (CLOB) rs.getClob(col);
java.lang.reflect.Method methodToInvoke = clob.getClass().getMethod(
"getCharacterOutputStream", (Class[]) null);
wr = (Writer) methodToInvoke.invoke(clob, (Object[]) null);
BufferedWriter bw = new BufferedWriter(wr);
bw.write(buf);
bw.flush();
bw.close();
conn.commit();
conn.close();
}
flag=true;
} catch (Exception ex){
try {
conn.rollback();
} catch (SQLException e) {
e.printStackTrace();
}
}
return flag;
}
标签:oracle,数据库,clob字段
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python实现删除列表中某个元素的3种方法
2023-02-08 16:01:59
Python基础之元编程知识总结
2023-03-17 11:06:36
Python转码问题的解决方法
2023-06-30 07:48:52
python读文件保存到字典,修改字典并写入新文件的实例
2021-09-19 05:37:57
关于python并发编程中的协程
2023-10-18 04:37:44
Pytorch中的Broadcasting问题
2022-10-03 06:26:58
session的存储方式和配置方法介绍
2022-04-28 05:48:59
Tableau连接mysql数据库的实现步骤
2024-01-21 20:50:41
![](https://img.aspxhome.com/file/2023/1/74781_0s.jpg)
Python日志:自定义输出字段 json格式输出方式
2022-08-20 01:27:19
![](https://img.aspxhome.com/file/2023/7/93857_0s.jpg)
解决Python对齐文本字符串问题
2021-08-20 10:22:26
php使用curl抓取qq空间的访客信息示例
2023-10-30 05:50:32
Python构建区块链的方法详解
2021-01-07 07:20:55
![](https://img.aspxhome.com/file/2023/5/99045_0s.png)
举例讲解Python常用模块
2022-03-21 07:35:49
js增强的自定义事件模型
2008-02-18 12:37:00
基于PyQt5制作一个windows通知管理器
2022-10-18 19:28:24
![](https://img.aspxhome.com/file/2023/7/75327_0s.gif)
在python中利用numpy求解多项式以及多项式拟合的方法
2021-05-14 03:01:24
深入SQL Server中char、varchar、text和nchar、nvarchar、ntext的区别详解
2024-01-26 11:26:57
大数据就业的三大方向和最热门十大岗位【推荐】
2023-05-18 16:21:06
![](https://img.aspxhome.com/file/2023/9/131789_0s.jpg)
6个卓越Web设计细节[译]
2010-03-24 18:34:00
![](https://img.aspxhome.com/file/UploadPic/20103/24/01-28s.jpg)
pytorch 实现tensor与numpy数组转换
2022-02-07 16:06:44