zoukankan      html  css  js  c++  java
  • java、oracle对CLOB处理

    oracle CLOB字段转换位VARCHAR

    1.实际上处理CLOB字段的时候,直接TO_CHAR,当长度超过4000的时候,会报错,提示列被截取;

    CLOB转varchar2:select to_char(CLOB字段) from table

    2.直接使用SUBSTR对CLOB字段进行截取,是不能起到任何作用的;

    3.可以使用dbms_lob.substr(clobcolumn,4000),对CLOB字段进行截取;截取的长度是4000还是2000根据存储的是汉字和数据决定长度;

    java获取oracle中CLOB字段,转换成String

    try {
    PreparedStatement stmt = session.connection().prepareStatement(sql); 
    ResultSet rs = stmt.executeQuery(); 
    while (rs.next()) 
    { 
    Clob clob = (Clob)rs.getObject(1); 
    result = ClobToString(clob);
    }
    
    } catch (HibernateException e) {
    e.printStackTrace();
    } catch (SQLException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    } finally {
    session.close();
    }
    
    //oracle.sql.Clob类型转换成String类型
    
    public String ClobToString(Clob clob) throws SQLException, IOException {
    
    String reString = "";
    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();
    return reString;
    }
    

      

  • 相关阅读:
    3.1C#中的命名空间
    2章总结
    2.4冒泡排序
    2.3 C#中的数组
    2.2二重循环
    2.1c#中的循环语句
    1章总结
    docker内外数据拷贝
    搭建docker环境
    centos7 部署Apache的httpd服务器
  • 原文地址:https://www.cnblogs.com/chenweichu/p/7701843.html
Copyright © 2011-2022 走看看