Java读取Oracle的Clob类型数据,由Clob转String

问题场景:

        从Oracle读取出来的字段是Clob类型数据,因需要对其进行操作,则转成字符串类型

调用方法:

Clob clobDDL = (Clob) map.get("ClobDDL");
String str = ClobToString(clobDDL);

转化方法:

    /**
     * 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;
    }

你可能感兴趣的:(java,oracle,开发语言)