Java 讀取clob字段的幾種方法

Java 讀取clob字段的幾種方法

1、第一種

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

2、第二種

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

3、第三種

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 + "\r\n";
   }
}

總結:java

  • 第一種方法代碼量少,且能避免中文亂碼問題;
  • 第二種方法與第一種方法效率差很少,也是常使用的一種方法;
  • 第三種方法效率極低,若是數據比較大的話建議不要使用。
相關文章
相關標籤/搜索