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"; }
}
總結:
- 第一種方法代碼量少,且能避免中文亂碼問題;
- 第二種方法與第一種方法效率差不多,也是常使用的一種方法;
- 第三種方法效率極低,如果數據比較大的話建議不要使用。