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

          導(dǎo)航

          <2010年10月>
          262728293012
          3456789
          10111213141516
          17181920212223
          24252627282930
          31123456
          統(tǒng)計(jì)
          • 隨筆 - 19
          • 文章 - 0
          • 評論 - 23
          • 引用 - 0

          常用鏈接

          留言簿(4)

          我參與的團(tuán)隊(duì)

          隨筆分類

          隨筆檔案

          我的好友

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

           

          環(huán)境:Hibernate 2.1.7 + MySQL4.1(MySQL的編碼已設(shè)置為utf8)
          問題:通過Hibernate向MySQL寫入中文后,通過Hibernate取回?cái)?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 閱讀(15951) 評論(13)  編輯  收藏 所屬分類: 技術(shù)積累
          Comments
          • # re: Hibernate MySQL 中文亂碼問題
            MDZhao
            Posted @ 2005-12-29 23:53
            我就是這樣呀,可是還是不行呀?  回復(fù)  更多評論   
          • # re: Hibernate MySQL 中文亂碼問題
            beherca
            Posted @ 2006-01-24 09:53
            謝謝啦,正好遇到這個(gè)問題,我是用struts+hibernate+mysql來作系統(tǒng)的,做了一個(gè)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");

            但用主鍵查單個(gè)時(shí)不出,提示查找不到,當(dāng)主鍵為中文時(shí):
            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
            為什么我的報(bào)錯(cuò)啊,我用的是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ù)  更多評論   
           
          主站蜘蛛池模板: 天门市| 大理市| 中山市| 城步| 邳州市| 澳门| 吉林市| 南漳县| 安陆市| 井陉县| 华容县| 丽江市| 章丘市| 时尚| 安远县| 顺义区| 富川| 禹州市| 扎囊县| 威远县| 牙克石市| 屏南县| 启东市| 济源市| 五常市| 积石山| 秦皇岛市| 塔河县| 汕尾市| 嘉鱼县| 瓦房店市| 亚东县| 郑州市| 栾城县| 尼木县| 南阳市| 富民县| 建昌县| 南涧| 湾仔区| 田林县|