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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM