String s1="1231dsdgasd的飒飒大";
Clob c = new SerialClob(s1.toCharArray());//String 转 clob
Blob b = new SerialBlob(s1.getBytes("GBK"));//String 转 blob// 也可以这样不传字符集名称,默认使用系统的// Blob b = new SerialBlob(s1.getBytes()); String clobString = c.getSubString(1, (int) c.length());//clob 转 String
String blobString = new String(b.getBytes(1, (int) b.length()),"GBK");//blob 转 String// 前面若没传入字符集名称,则这里也不需要传入,以免出错// String blobString = new String(b.getBytes(1, (int) b.length())); System.out.println(clobString);
System.out.println(blobString);
/**
* Blob类型 转 String类型
* @param blob
* @return
* Blob类型 转 String类型
* @param blob
* @return
*import java.sql.Blob;
*/
public static String blobToString(Blob blob){
String result = "";
if ( blob != null ){
InputStream is;
try {
is = blob.getBinaryStream();
ByteArrayInputStream bais = (ByteArrayInputStream)is;
byte[] byte_data = new byte[bais.available()];
bais.read(byte_data,0,byte_data.length);
result = new String(byte_data,"utf-8");
is.close();
} catch (SQLException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
*/
public static String blobToString(Blob blob){
String result = "";
if ( blob != null ){
InputStream is;
try {
is = blob.getBinaryStream();
ByteArrayInputStream bais = (ByteArrayInputStream)is;
byte[] byte_data = new byte[bais.available()];
bais.read(byte_data,0,byte_data.length);
result = new String(byte_data,"utf-8");
is.close();
} catch (SQLException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
本文详细介绍了如何在MySQL数据库中将Clob和Blob数据类型转换为String,包括Java编程中的处理方法,涵盖了读取、转换及存储过程中的关键步骤和示例代码。
1823

被折叠的 条评论
为什么被折叠?



