Duran's technical life
          踏踏實(shí)實(shí)學(xué)技術(shù),認(rèn)認(rèn)真真做研究。

          環(huán)境:Hibernate 2.1.7 + MySQL4.1(MySQL的編碼已設(shè)置為utf8)
          問(wèn)題:通過(guò)Hibernate向MySQL寫(xiě)入中文后,通過(guò)Hibernate取回?cái)?shù)據(jù),在console中打印java對(duì)象顯示正常。但在MySQL Query Browser中看到的是亂碼,傳給前端的Flex也是亂碼。
          原因:Hibernate的基礎(chǔ)還是JDBC,所以一樣需要設(shè)置characterEncoding!
          解決方法
          在hibernate.cfg.xml中應(yīng)該這樣寫(xiě)

          <property name="connection.url">jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=UTF-8</property>
          如果用hibernate.properties
          #hibernate.connection.url jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8

          ps:處理中文的話,characterEncoding用GBK一樣可以。
          但字節(jié)編碼問(wèn)題最好的解決方法還是統(tǒng)一使用UTF-8?。?!
          posted on 2005-06-10 21:35 Duran's technical life 閱讀(15950) 評(píng)論(13)  編輯  收藏 所屬分類(lèi): 技術(shù)積累
          Comments
          • # re: Hibernate MySQL 中文亂碼問(wèn)題
            MDZhao
            Posted @ 2005-12-29 23:53
            我就是這樣呀,可是還是不行呀?  回復(fù)  更多評(píng)論   
          • # re: Hibernate MySQL 中文亂碼問(wèn)題
            beherca
            Posted @ 2006-01-24 09:53
            謝謝啦,正好遇到這個(gè)問(wèn)題,我是用struts+hibernate+mysql來(lái)作系統(tǒng)的,做了一個(gè)filter解決了struts的亂碼,然后修改了mysql的默認(rèn)編碼,但是發(fā)現(xiàn)還是出現(xiàn)亂碼的問(wèn)題。呵呵,原來(lái)是這樣的~~謝謝  回復(fù)  更多評(píng)論   
          • # re: Hibernate MySQL 中文亂碼問(wèn)題
            antonlan
            Posted @ 2006-06-08 22:49
            banq:

            像你如上的設(shè)置,使用程序讀取和插入是沒(méi)有問(wèn)題的;但是如果我要用sql插入中文就不成了,我用過(guò)mysql的命令行和其它的客戶端工具,都插不進(jìn)去。設(shè)置成GBK就沒(méi)問(wèn)題了。  回復(fù)  更多評(píng)論   
          • # re: Hibernate MySQL 中文亂碼問(wèn)題
            板橋居
            Posted @ 2006-07-13 14:45
            @antonlan
            if u excute a script loaded form a local file, the file should also be encoded by UTF8(UEdit can change the encode)!  回復(fù)  更多評(píng)論   
          • # re: Hibernate MySQL 中文亂碼問(wèn)題
            hohoho
            Posted @ 2006-07-31 23:34
            &amp;useUnicode=true&amp;characterEncoding=utf-8&amp;mysqlEncoding=utf8

            mysql也設(shè)置成utf8,一切ok  回復(fù)  更多評(píng)論   
          • # re: Hibernate MySQL 中文亂碼問(wèn)題
            andy[匿名]
            Posted @ 2006-12-05 14:06
            查詢列表沒(méi)有問(wèn)題,
            (List)this.getHibernateTemplate().find("from Customer");

            但用主鍵查單個(gè)時(shí)不出,提示查找不到,當(dāng)主鍵為中文時(shí):
            customercode參數(shù)為jsp(jsp也是用的utf-8,中文顯示也正常)
            傳入,如:/xxx/xx.do?customercode=移動(dòng)
            (Customer)getHibernateTemplate().load(Customer.class,customercode);

              回復(fù)  更多評(píng)論   
          • # re: Hibernate MySQL 中文亂碼問(wèn)題
            小楊
            Posted @ 2008-06-05 09:06
            以上的用法果然成效,大家可以試試  回復(fù)  更多評(píng)論   
          • # re: Hibernate MySQL 中文亂碼問(wèn)題
            小楊
            Posted @ 2008-06-05 09:07
            我的開(kāi)發(fā)環(huán)境是Struts+hibernate  回復(fù)  更多評(píng)論   
          • # re: Hibernate MySQL 中文亂碼問(wèn)題
            小董
            Posted @ 2009-05-14 20:34
            為什么我的報(bào)錯(cuò)啊,我用的是SQL Server 2000,hibernate,struts

            java.sql.SQLException: 無(wú)法打開(kāi)登錄 'OnlineExam?useUnicode=true&characterEncoding=UTF-8' 中請(qǐng)求的數(shù)據(jù)庫(kù)。登錄失敗。
              回復(fù)  更多評(píng)論   
          • # re: Hibernate MySQL 中文亂碼問(wèn)題[未登錄](méi)
            test
            Posted @ 2010-10-18 17:42
            很感謝@小董
              回復(fù)  更多評(píng)論   
          • # re: Hibernate MySQL 中文亂碼問(wèn)題
            xc
            Posted @ 2011-12-08 11:16
            幸虧看到樓主的解決方法;找了快一天的問(wèn)題;坑爹的編碼問(wèn)題  回復(fù)  更多評(píng)論   
          • # re: Hibernate MySQL 中文亂碼問(wèn)題
            仰泳...
            Posted @ 2012-05-23 08:44
            問(wèn)題解決了,謝謝幫助!!!  回復(fù)  更多評(píng)論   
          • # re: Hibernate MySQL 中文亂碼問(wèn)題[未登錄](méi)
            張龍
            Posted @ 2013-04-30 13:09
            我的怎么還沒(méi)用呢?  回復(fù)  更多評(píng)論   
           
          主站蜘蛛池模板: 稻城县| 农安县| 容城县| 金寨县| 秭归县| 余干县| 永寿县| 广灵县| 金门县| 辰溪县| 普格县| 略阳县| 南木林县| 偃师市| 上思县| 日喀则市| 大同市| 临夏县| 德格县| 定远县| 富裕县| 和田市| 郁南县| 宿迁市| 朝阳区| 鹿邑县| 陵水| 郴州市| 五家渠市| 天津市| 天峨县| 平安县| 山西省| 周宁县| 石首市| 罗山县| 马关县| 荣昌县| 张家界市| 道孚县| 东兴市|