1.结果集能得到byte[]类型的数据
2.String可以设置编码格式
3.读写完毕都要关闭流input、output
public Object nullSafeGet(ResultSet rs, String[] names, Object entity) throws HibernateException, SQLException {
String result = null;
byte[] bs = rs.getBytes(names[0]);
if(bs == null)
return result;
//解压字节流为字符串
ZipInputStream zipins = new ZipInputStream(new ByteArrayInputStream(bs));
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int read = -1;
try {
zipins.getNextEntry();
while((read=zipins.read(buf))>0){
out.write(buf, 0, read);
}
result = new String(out.toByteArray(), "GBK");
out.close();
zipins.close();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public void nullSafeSet(PreparedStatement ps, Object value, int index) throws HibernateException, SQLException {
String values = (String)value;
if(values == null){
ps.setObject(index, null);
return;
}
//把字符串压缩为字节流储存
ByteArrayOutputStream out = new ByteArrayOutputStream();
ZipOutputStream zipout = new ZipOutputStream(out);
try {
zipout.putNextEntry(new ZipEntry("s"));
zipout.write(values.getBytes("GBK"));
zipout.close();
out.close();
ps.setObject(index, out.toByteArray());
} catch (Exception e) {
e.printStackTrace();
}
}