學海拾遺

          生活、技術、思想無處不在學習
          posts - 52, comments - 23, trackbacks - 0, articles - 3
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          MySQL的中文問題

          Posted on 2007-07-08 21:12 tanzek 閱讀(1816) 評論(2)  編輯  收藏

          唉,看到網上這么多的關于MySQL中文編碼的問題。今天自己碰到了,網上沒有找到我的完整答案,發現真的是好暈啊!~
          ??? 不過幸好弄出來了,沒有讓整個下午的時候白費哦。
          就今天所看到的,和自己所操作的一起總結下哦。
          我用的工具有:

          MyEclipse 5.1.0
          JDK 1.6.0
          mysql
          - connection - java - 3.0 . 2
          MySQL? 5.0 . 27

          這里工具的版本是很重要的,呆會一點一點把它講來,所以把全部環境都列出來了!~

          1、data too long for column問題
          問題描述:在MySQL下面輸入select, insert等SQL語句,對于漢字均出現此種情況
          ????字段為char或varchar型,長度為255,絕對不會是真的字段長度不夠的問題。請大家查看這篇貼子:http://blog.sina.com.cn/u/53b0d5dc0100097v,有此問題的詳解。我后來的問題也基本上是在此篇基本上解決的。重要的是:--default-character-set=utf8參數

          2、語句在MySQL CMD下面輸入正確,但是在Java中執行同樣的SQL語句,卻無法工作。
          問題描述:如果SQL語句中帶中文的話,就會出現下面的異常,

          java.sql.SQLException:?Syntax?error?or?access?violation,??message?from?server:? " You?have?an?error?in?your?SQL?syntax;?check?the?manual?that?corresponds?to?your?MySQL?server?version?for?the?right?syntax?to?use?near?''中??at?line?1 "
          ?at?com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:
          1962 )
          ?at?com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:
          1163 )
          ?at?com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:
          1257 )
          ?at?com.mysql.jdbc.MysqlIO.sqlQuery(MysqlIO.java:
          1218 )?

          進行好上面第一點的配置以后還是無濟于事。
          ??? 通過細心觀察,總覺得不應該是MySQL的配置問題了,因為在MySQL CMD下面能夠很正常地輸入,所以懷疑是mysql connector的問題。
          ??? 查閱了mysql connector的docs后,找到了一些內容:
          All?strings?sent?from?the?JDBC?driver?to?the?server?are?converted?automatically?from?native?Java?Unicode?form?to?the?client?character?encoding,?including?all?queries?sent?via?Statement.execute(),?Statement.executeUpdate(),?Statement.executeQuery()?as?well?as?all?PreparedStatement?and?CallableStatement?parameters?with?the?exclusion?of?parameters?set?using?setBytes(),?setBinaryStream(),?setAsiiStream(),?setUnicodeStream()?and?setBlob().
          意思就是說:所有從JDBC驅動器到服務器的字符串都將會自動地從本地JAVA Unicode編碼形式轉換為客戶端的字符編碼。

          Prior?to?MySQL?Server?4.1,?Connector/J?supported?a?single?character?encoding?per?connection,?which?could?either?be?automatically?detected?from?the?server?configuration,?or?could?be?configured?by?the?user?through?the?useUnicode?and?characterEncoding?properties.

          Starting?with?MySQL?Server?
          4.1,?Connector/J?supports?a?single?character?encoding?between?client?and?server,?and?any?number?of?character?encodings?for?data?returned?by?the?server?to?the?client?in?ResultSets.
          在這里就涉及到了版本的問題:在MySQL 4.1以前的版本中,連接器對每一個connection支持單個的字符編碼;而在MySQL 4.1開始以后的版本中,連接器在客戶端和服務器端之間支持單個的字符編碼。

          重要的就是在這里了,不知道因為是什么原因,我的連接器客戶端的編碼總是與服務器的對接不上,因此在這里將采用手動指定客戶端編碼。如下所述:
          To?override?the?automatically-detected?encoding?on?the?client?side,?use?the?characterEncoding?property?in?the?URL?used?to?connect?to?the?server.

          對于useUnicode和characterEncoding兩個連接屬性,下面介紹一點點:
          useUnicodeShould the driver use Unicode character encodings when handling strings? Should only be used when the driver can't determine the character set mapping, or you are trying to 'force' the driver to use a character set that MySQL either doesn't natively support (such as UTF-8), true/false, defaults to 'true'
          characterEncodingIf 'useUnicode' is set to true, what character encoding should the driver use when dealing with strings? (defaults is to 'autodetect')
          因此,在連接中,我把連接的URL 從原來的:
          jdbc:mysql://localhost:3306/[DBName]","[username]","[password]" 改成為:jdbc:mysql://localhost:3306/[DBname]?useUnicode=true&characterEncoding=UTF-8","[username]","[password]"。

          經過這樣子的改動以后,程序能夠正常地輸入中文值或作為條件參數。

          下面還有關于連接器的一點點內容:
          To?allow?multiple?character?sets?to?be?sent?from?the?client,?the?"UTF-8"?encoding?should?be?used,?either?by?configuring?"utf8"?as?the?default?server?character?set,?or?by?configuring?the?JDBC?driver?to?use?"UTF-8"?through?the?characterEncoding?property.


          總結:
          此問題可能還在其它地方有問題,但是到現在為止,中文不能出現在SQL語句中的問題已經解決。如果有網友能夠有進一步地突破,還希望不吝指教。謝謝了!~? 當然,如果本篇有一些不足之處,也請指出哦!

          評論

          # re: MySQL的中文問題  回復  更多評論   

          2007-08-30 20:08 by summerbell
          在兩個月之后的今天,解決了我的問題:)

          # re: MySQL的中文問題  回復  更多評論   

          2008-05-23 21:26 by 小斤
          謝謝你的文章,解決了我的難題

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 丽江市| 呼和浩特市| 盘锦市| 承德县| 华蓥市| 扶沟县| 乌兰察布市| 桃源县| 弥渡县| 莎车县| 调兵山市| 龙泉市| 岳西县| 榆社县| 库尔勒市| 寿阳县| 龙山县| 昌图县| 囊谦县| 安丘市| 台湾省| 泸定县| 沭阳县| 墨玉县| 石门县| 庆阳市| 太仓市| 博白县| 武安市| 庆安县| 永新县| 延长县| 新竹县| 河曲县| 巴彦淖尔市| 祁门县| 新干县| 新昌县| 铁力市| 卓资县| 玛多县|