Clob 转String 类型使用 以及转码问题

来源:互联网 发布:西安软件开发 大专 编辑:程序博客网 时间:2024/05/22 12:02
public class ColbUtil {


/**
* Clob 类型转换成String类型

* @param clob
* @return String 转换后的字符串
* @throws Exception
*/
public static  String clobString(Clob clob) throws Exception {
if(clob == null) {
return "";
}

BufferedReader br = null;
String str1 = "";
String str2 = null;
Reader is;
try {
is = clob.getCharacterStream();
br = new BufferedReader(is);
while ((str2 = br.readLine()) != null) {
str1 = str1 + str2;
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (br != null) {
br.close();
}
}
return str1;
}


public static  String toUtf8String(String s) throws UnsupportedEncodingException { 
StringBuffer sb = new StringBuffer();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c >= 0 && c <= 255) {
sb.append(c);
} else {
byte[] b;
try {
b = Character.toString(c).getBytes("utf-8");
} catch (Exception ex) {
b = new byte[0];
}
for (int j = 0; j < b.length; j++) {
int k = b[j];
if (k < 0)
k += 256;
sb.append("%" + Integer.toHexString(k).toUpperCase());
}
}
}
return sb.toString();
  }


}
0 0
原创粉丝点击