java读取clob字段的几种方法

最近频繁处理clob字段,故集中了几种读取clob字段的方法,供大家参考。

   第一种:

    Clob clob = rs.getClob("remark");//java.sql.Clob
    String detailinfo = "";
    if(clob != null){
     detailinfo = clob.getSubString((long)1,(int)clob.length());
    }


    第二种:

    Clob clob = rs.getClob("remark");//java.sql.Clob
    int i = 0;
    if(clob != null){
     InputStream input = clob.getAsciiStream();
     int len = (int)clob.length();
     byte by[] = new byte[len];
     while(-1 != (i = input.read(by, 0, by.length))){
      input.read(by, 0, i);
     }
     detailinfo = new String(by, "utf-8");
    }
    

    第三种:

   Clob clob = rs.getClob("remark");//java.sql.Clob
    String value="";
    String line="";
    if(clob!=null){
           Reader reader=((oracle.sql.CLOB)clob).getCharacterStream();
           BufferedReader br=new BufferedReader(reader);
           while((line=br.readLine())!=null)
           {
            value += line + " ";
           }

    } 

    第一种方法代码量少,且能避免中文乱码问题;第二种方法与第一种方法效率差不多,也是常使用的一种方法;第三种方法效率极低,如果数据比较大的话建议不要使用。

    本人把100页的一个word文档的内容通过FCK录入到数据库的一个clob字段中,然后分别通过上面三种方法读取,前两种方法用的时间差不多都是4秒,第三种方法用了三分多钟。不过前两种方法没有考虑数据非常大的情况,一般情况下是够用了(一部小说没问题)。

原文地址:https://www.cnblogs.com/waying/p/4101473.html