DBUtil類中的一個靜態方法:
/**
* 將CLOB轉成String ,靜態方法
* @param clob 字段
* @return 內容字串,如果出現錯誤,返回null
*/
public final static String clob2String(Clob clob)
{
if (clob == null)
{
return null;
}
StringBuffer sb = new StringBuffer(65535);//64K
Reader clobStream = null;//創建一個輸入流對象
try
{
clobStream = clob.getCharacterStream();
char[] b = new char[60000];//每次獲取60K
int i = 0;
while((i = clobStream.read(b)) != -1)
{
sb.append(b,0,i);
}
}
catch(Exception ex)
{
sb = null;
}
finally
{
try
{
if (clobStream != null)
clobStream.close();
}
catch (Exception e)
{
}
}
if (sb == null)
return null;
else
return sb.toString();
}
//row是一個對象(JAVABEAN)類
有個private String topic 屬性;
rs 是一個ResultSet
row.setTopic(DBUtil.clob2String((Clob)rs.getClob("topic")));