Hibernate使用者自定義資料型別問題

zgl1984829發表於2006-07-27
各位大俠,我專案的資料庫是oracle字符集是UTF-8
資料庫中使用Clob儲存資料。
我自己寫了一個自定義資料型別
public class StringClobType implements UserType {
public Object nullSafeGet(ResultSet arg0, String[] arg1, Object arg2)
throws HibernateException, SQLException {
String ret = null;
StringBuffer buffer = new StringBuffer();
try {
// First we get the stream
InputStream is = arg0.getAsciiStream(arg1[0]);
byte[] buf = new byte[1024];
int read = -1;

while ((read = is.read(buf)) > 0) {
buffer.append(new String(buf, 0, read,"UTF-8"));
}
is.close();
} catch (IOException ioe) {
ioe.printStackTrace();
throw new HibernateException("Unable to read from resultset", ioe);
}
ret = buffer.toString();
return ret;
}

/*
* (non-Javadoc) (at) see org (dot) hibernate.usertype.UserTypenullSafeSet
* (java.sql.PreparedStatement, java.lang.Object, int)
*/
public void nullSafeSet(PreparedStatement pst, Object data, int index)
throws HibernateException, SQLException {
data = data == null ? new String() : data;
String in = (String) data;

byte[] buf = null;
try {
buf = in.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
buf = in.getBytes();
throw new HibernateException(e.getMessage());
}
int len = buf.length;

ByteArrayInputStream bais = new ByteArrayInputStream(buf);

pst.setAsciiStream(index, bais, len);

}
我的問題是,存入資料庫的資料漢字全部變成了亂碼!請問哪位知道問題何在

相關文章