Java 将Clob字段转换成String字符串

一、使用JDBC数据源获取的Clob字段转换String字符串。

public static Object clobToString(Object in) throws Exception {
        try{
            if ("oracle.sql.CLOB".equals(in.getClass().getName())) {
                String rtn = "";
                oracle.sql.CLOB clob = (oracle.sql.CLOB) in;
                InputStream input = clob.getAsciiStream();
                int len = (int) clob.length();
                byte[] by = new byte[len];
                int i;
                while (-1 != (i = input.read(by, 0, by.length))) {
                    input.read(by, 0, i);
                }
                rtn = new String(by);
                rtn = clob.getSubString((long) 1, (int) clob.length());

                return rtn;
            }
        }catch (Exception e) {
            // TODO: handle exception
            return in;
        }
        
    }

二、使用WebLogic数据源获取的Clob字段转换String字符串。

public static Object clobToString1(Object in) throws Exception {

        try {
            if ("weblogic.jdbc.wrapper.Clob_oracle_sql_CLOB".equals(in.getClass().getName())) {
                String rtn = "";
                Method method = in.getClass().getMethod("getVendorObj",new Class[] {});
                oracle.sql.CLOB clob = (oracle.sql.CLOB) method.invoke(in);
                InputStream input = clob.getAsciiStream();
                int len = (int) clob.length();
                byte[] by = new byte[len];
                int i;
                while (-1 != (i = input.read(by, 0, by.length))) {
                    input.read(by, 0, i);
                }
                rtn = new String(by);
                rtn = clob.getSubString((long) 1, (int) clob.length());

                return rtn;
            }
        } catch (Exception e) {
            // TODO: handle exception
            return in;
        }
    }
原文地址:https://www.cnblogs.com/sinosoft/p/13500590.html