Hibernate使用者自定義資料型別問題
各位大俠,我專案的資料庫是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);
}
我的問題是,存入資料庫的資料漢字全部變成了亂碼!請問哪位知道問題何在
資料庫中使用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);
}
我的問題是,存入資料庫的資料漢字全部變成了亂碼!請問哪位知道問題何在
相關文章
- 自定義資料型別資料型別
- DM自定義資料型別資料型別
- SQL Server 中自定義資料型別SQLServer資料型別
- HIBERNATE的自定義型別主鍵的對映??型別
- 自主資料型別:在TVM中啟用自定義資料型別探索資料型別
- 自定義型別型別
- Hadoop-MapReduce之自定義資料型別Hadoop資料型別
- Python 多程式的自定義共享資料型別Python資料型別
- Kettle自定義資料庫連線型別連線HGDB資料庫型別
- MapReduce程式設計實踐之自定義資料型別程式設計資料型別
- 型別自定義格式字串型別字串
- Pl/SQL 自定義型別SQL型別
- ORACLE 自定義型別[轉]Oracle型別
- PLSQL Language Reference-PL/SQL資料型別-SQL資料型別-使用者定義的PL/SQL子型別SQL資料型別
- UnrealEngine建立自定義資產型別Unreal型別
- Linq to sql 自定義型別SQL型別
- ros|自定義訊息型別ROS型別
- long資料型別跨平臺問題資料型別
- 【填坑往事】使用Rxjava2的distinct操作符處理自定義資料型別去重的問題RxJava資料型別
- Hibernate 自定義主鍵
- android自定義Application全域性變數不能型別轉換的問題AndroidAPP變數型別
- 建立自定義塊 - 型別檢查型別
- Hibernate 資料同步問題請教
- MySQL修改資料型別的問題總結MySql資料型別
- 【Go學習筆記6】使用者自定義型別和方法Go筆記型別
- 使用者自定義控制元件拖拽失敗問題控制元件
- HIBERNATE的對映---資料庫表中欄位和對應持久化類中屬性都是自定義型別的?資料庫持久化型別
- C# 泛型集合的自定義型別排序C#泛型型別排序
- JumpList中Recent類別和自定義型別薦型別
- Android 自定義構建型別 BuildTypeAndroid型別UI
- MyBatis使用自定義TypeHandler轉換型別MyBatis型別
- C語言筆記——自定義型別C語言筆記型別
- 兄弟連go教程(7)自定義型別Go型別
- EF:自定義Oracle的對映型別Oracle型別
- MyBatis 基本資料型別條件判斷問題MyBatis資料型別
- java中文問題和資料庫型別有關Java資料庫型別
- hibernate對映檔案生成資料庫指令碼過程,sequence 設定初始值 以及自定義增量問題資料庫指令碼
- struts 自定義validate 問題