赞
踩
目录
问题场景:
从Oracle读取出来的字段是Clob类型数据,因需要对其进行操作,则转成字符串类型
调用方法:
- Clob clobDDL = (Clob) map.get("ClobDDL");
- String str = ClobToString(clobDDL);
转化方法:
可以通过stream流将clob字段,拼接起来,这样对于一些简单的数据是没有问题的,但是有时候会碰到clob字段的数据,带一些特殊字符,比如换行、空格等格式的话,解析出来的数据是不带空格和换行的,那这样的数据就是有问题的,和原来的不符合
- /**
- * clob转String
- *
- * @param clob
- * @return
- */
- public String ClobToString(Clob clob) {
- String reString = "";
- try {
- 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();
- } catch (Exception e) {
- log.error("Oracle数据由Clob类型转化String类型处理失败 原因={}", e.getMessage());
- throw new BizException("Oracle数据处理失败");
- }
- return reString;
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
可以直接通过clob的提供的方法进行处理,非常的方便,但是一定要注意判空,通过下面的这种方式处理,即使有换行、空格等特殊字符出现的时候,都能够原模原样的保存到新的字段当中
比如判空处理:
- /**
- * clob转String
- *
- * @param clob
- * @return
- */
- public String ClobToString(Clob clob) {
- String reString = "";
- try {
-
- reString = clob.getSubString((long) 1, (int) clob.length());
-
- } catch (Exception e) {
- log.error("Oracle数据由Clob类型转化String类型处理失败 原因={}", e.getMessage());
- throw new BizException("Oracle数据处理失败");
- }
- return reString;
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。