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

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

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

            像你如上的設(shè)置,使用程序讀取和插入是沒有問題的;但是如果我要用sql插入中文就不成了,我用過mysql的命令行和其它的客戶端工具,都插不進(jìn)去。設(shè)置成GBK就沒問題了。  回復(fù)  更多評論   
          • # re: Hibernate MySQL 中文亂碼問題
            板橋居
            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ù)  更多評論   
          • # re: Hibernate MySQL 中文亂碼問題
            hohoho
            Posted @ 2006-07-31 23:34
            &amp;useUnicode=true&amp;characterEncoding=utf-8&amp;mysqlEncoding=utf8

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

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

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

            java.sql.SQLException: 無法打開登錄 'OnlineExam?useUnicode=true&characterEncoding=UTF-8' 中請求的數(shù)據(jù)庫。登錄失敗。
              回復(fù)  更多評論   
          • # re: Hibernate MySQL 中文亂碼問題[未登錄]
            test
            Posted @ 2010-10-18 17:42
            很感謝@小董
              回復(fù)  更多評論   
          • # re: Hibernate MySQL 中文亂碼問題
            xc
            Posted @ 2011-12-08 11:16
            幸虧看到樓主的解決方法;找了快一天的問題;坑爹的編碼問題  回復(fù)  更多評論   
          • # re: Hibernate MySQL 中文亂碼問題
            仰泳...
            Posted @ 2012-05-23 08:44
            問題解決了,謝謝幫助!!!  回復(fù)  更多評論   
          • # re: Hibernate MySQL 中文亂碼問題[未登錄]
            張龍
            Posted @ 2013-04-30 13:09
            我的怎么還沒用呢?  回復(fù)  更多評論   
           
          主站蜘蛛池模板: 大宁县| 西充县| 柏乡县| 陆川县| 海阳市| 师宗县| 平昌县| 灵璧县| 揭阳市| 家居| 枣庄市| 铜鼓县| 什邡市| 会泽县| 阿坝| 桐柏县| 江北区| 扶沟县| 昌邑市| 额济纳旗| 松阳县| 井陉县| 嘉义市| 康定县| 曲沃县| 云南省| 庄河市| 神木县| 盐城市| 顺平县| 筠连县| 克拉玛依市| 奉化市| 合江县| 酒泉市| 虎林市| 内黄县| 高要市| 乌鲁木齐县| 宜兴市| 柳河县|