非淡泊无以明志,非宁静无以致远。非学无以广才,非志无以成学。

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

 

posted @ 2020-08-14 10:02  Happy丶小鱼  阅读(2518)  评论(0编辑  收藏  举报