Duran's technical life
          踏踏實實學技術,認認真真做研究。

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

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

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

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

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

              回復  更多評論   
          • # re: Hibernate MySQL 中文亂碼問題
            小楊
            Posted @ 2008-06-05 09:06
            以上的用法果然成效,大家可以試試  回復  更多評論   
          • # re: Hibernate MySQL 中文亂碼問題
            小楊
            Posted @ 2008-06-05 09:07
            我的開發(fā)環(huán)境是Struts+hibernate  回復  更多評論   
          • # 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ù)庫。登錄失敗。
              回復  更多評論   
          • # re: Hibernate MySQL 中文亂碼問題[未登錄]
            test
            Posted @ 2010-10-18 17:42
            很感謝@小董
              回復  更多評論   
          • # re: Hibernate MySQL 中文亂碼問題
            xc
            Posted @ 2011-12-08 11:16
            幸虧看到樓主的解決方法;找了快一天的問題;坑爹的編碼問題  回復  更多評論   
          • # re: Hibernate MySQL 中文亂碼問題
            仰泳...
            Posted @ 2012-05-23 08:44
            問題解決了,謝謝幫助!!!  回復  更多評論   
          • # re: Hibernate MySQL 中文亂碼問題[未登錄]
            張龍
            Posted @ 2013-04-30 13:09
            我的怎么還沒用呢?  回復  更多評論   
           
          主站蜘蛛池模板: 兴安盟| 夹江县| 乌鲁木齐县| 清远市| 宁陵县| 元氏县| 龙陵县| 淄博市| 金塔县| 鱼台县| 舒城县| 木里| 通化市| 清镇市| 南丰县| 海丰县| 哈密市| 乌海市| 满城县| 德惠市| 秦皇岛市| 鄂伦春自治旗| 玉山县| 镶黄旗| 广宁县| 乌兰察布市| 漾濞| 贡觉县| 镇江市| 白银市| 元江| 瑞昌市| 伊金霍洛旗| 福泉市| 台山市| 沙雅县| 肥乡县| 新建县| 蓝田县| 青浦区| 上饶市|