String s1="1234abc我是大字段CLOB或BOLB";
- String转大字段
//String 转 clob
Clob c = new SerialClob(s1.toCharArray());
//String 转 blob
Blob b = new SerialBlob(s1.getBytes("GBK"));
//也可以这样不传字符集名称,使用系统默认的
//Blob b = new SerialBlob(s1.getBytes());
- 大字段转String
//clob 转 String
String clobStri = c.getSubString(1, (int) c.length());
//blob 转 String
String blobString = new String(b.getBytes(1, (int) b.length()),"GBK");
//前面若没传入字符集名称,则这里也不需要传入,以免出错
//String blobString = new String(b.getBytes(1, (int) b.length()));
System.out.println(clobString);
System.out.println(blobString);
- 转换函数
public static String convertToString(Object o){
if(o == null)
return null;
if(o instanceof String){
return (String)o;
}else if(o instanceof Integer){
return o.toString();
}else if(o instanceof SerializableClob){
SerializableClob clob = (SerializableClob) o ;
try {
return clob.getSubString(1, (int) clob.length());
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
return o.toString();
}
这种数据库的字段类型转成Java字段类型的行为,从框架上来说叫做orm(object-relative-model)对象关系模型