Oracle中将Clob字段转换成字符串

您所在的位置:网站首页 数据库clob转字符串 Oracle中将Clob字段转换成字符串

Oracle中将Clob字段转换成字符串

2024-07-11 22:02| 来源: 网络整理| 查看: 265

1. 利用dbms_lob.substr()方法可将对应字段转换成字符串如下

select dbms_lob.substr(content) from NEWS

该方法有个缺点,当content字段长度超过某个值时,会报错。

2.获取Clob对象,在Java中通过对流处理获取字段内容,该方式没有长度限制

select content from NEWS // 将字CLOB转成STRING类型 public String ClobToString(Clob clob) throws SQLException, IOException { String reString = ""; java.io.Reader is = clob.getCharacterStream();// 得到流 BufferedReader br = new BufferedReader(is); String s = br.readLine(); StringBuffer sb = new StringBuffer(); while (s != null) {// 执行循环将字符串全部取出付值给StringBuffer由StringBuffer转成STRING sb.append(s); s = br.readLine(); } reString = sb.toString(); return reString; }

接下来的重点是将从数据库获取到的该字段的对象转换成Clob对象,如下:

String content = ClobToString((Clob)obj[1]);

其中我的obj是从数据库获取的字段数组,obj[1]对应该Clob对象。



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3