Java 讀取clob字段的幾種方法


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

總結:

  • 第一種方法代碼量少,且能避免中文亂碼問題;
  • 第二種方法與第一種方法效率差不多,也是常使用的一種方法;
  • 第三種方法效率極低,如果數據比較大的話建議不要使用。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM