zoukankan      html  css  js  c++  java
  • 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;
            }
        }
  • 相关阅读:
    Java Stream 流(JDK 8 新特性)
    Java EnumMap 实现类
    Java 设计模式
    Java lambda 表达式详解(JDK 8 新特性)
    Java forEach 方式遍历集合(Java 8 新特性)
    Java 单例设计模式
    Java public 和 private 访问修饰符
    == 、equals 、hashcode
    String
    ClassLoader 的分类及加载顺序
  • 原文地址:https://www.cnblogs.com/sinosoft/p/13500590.html
Copyright © 2011-2022 走看看