Hibernate用户自定义数据类型问题

06-07-27 zgl1984829
各位大侠,我项目的数据库是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.UserType#nullSafeSet

* (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);

}

我的问题是,存入数据库的数据汉字全部变成了乱码!请问哪位知道问题何在

猜你喜欢