zoukankan      html  css  js  c++  java
  • oracle-解析CLOB格式字段转String

    SQL CLOB 是 内置类型,它将字符大对象 (Character Large Object) 存储为数据库表某一行中的一个列值。默认情况下, 驱动程序使用 SQL locator(CLOB) 实现 Clob 对象,这意味着 CLOB 对象包含一个指向 SQL CLOB 数据的逻辑 指针而不是数据本身。Clob 对象在它被创建的事务处理期间有效。
    在一些 数据库系统里,也使用Text 作为CLOB的别名,比如SQL Server。
    Oracle数据库中如何将Clob查出并转换为String呢,有如下两个方法:
    1、 --SQL 语句
    select DBMS_LOB.SUBSTR(content,4000,1) ||   
    
           DBMS_LOB.SUBSTR(content,4000,4001) ||   
    
           DBMS_LOB.SUBSTR(content,4000,8001) ||   
    
           DBMS_LOB.SUBSTR(content,4000,12001)   
    
    from xxx;

    2、java中将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;
    }

    原文地址:https://www.linuxidc.com/Linux/2012-02/53393.htm

  • 相关阅读:
    StructuredStreaming整合Kafka
    StructuredStreaming-Sink
    ValueError: max() arg is an empty sequence
    StructuredStreaming
    学习进度报告08
    SparkSQL+Hive
    SparkSQL花式查询
    RDD-DF-DS相互转换
    SparkSQL学习
    学习进度报告07
  • 原文地址:https://www.cnblogs.com/shui68home/p/15414406.html
Copyright © 2011-2022 走看看