<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    學海拾遺

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

    MySQL的中文問題

    Posted on 2007-07-08 21:12 tanzek 閱讀(1812) 評論(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 小斤
    謝謝你的文章,解決了我的難題

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


    網站導航:
     
    主站蜘蛛池模板: 麻豆一区二区三区蜜桃免费| xvideos永久免费入口| 曰皮全部过程视频免费国产30分钟 | 国产亚洲美女精品久久久久狼| 久久久久国产精品免费免费不卡 | 久久精品国产精品亚洲| 久久久久久AV无码免费网站下载| 亚洲xxxx18| 中文字幕亚洲天堂| 欧亚精品一区三区免费| 国产精品成人啪精品视频免费| 亚洲日本国产精华液| 亚洲毛片av日韩av无码| 免费人成在线观看69式小视频| 全部在线播放免费毛片| 亚洲一本之道高清乱码| 亚洲热线99精品视频| 免费毛片在线看片免费丝瓜视频 | 亚洲嫩草影院在线观看| 久久精品国产精品亚洲| 成年女性特黄午夜视频免费看| 日韩电影免费在线观看网站| 亚洲欧美第一成人网站7777| 亚洲伦另类中文字幕| 亚洲Av无码国产情品久久| 国产精品久久久久久久久免费| 久久久久久噜噜精品免费直播| 亚洲日本VA中文字幕久久道具| 久久精品国产亚洲香蕉| 亚洲午夜激情视频| 在线观看永久免费视频网站| 日本视频一区在线观看免费| 最近免费中文字幕中文高清| 男性gay黄免费网站| 国产成人亚洲综合一区| 久久久久久久亚洲Av无码| 亚洲精品国产品国语在线 | 亚洲av无码成人影院一区| 亚洲婷婷在线视频| 91亚洲导航深夜福利| 国产亚洲精品美女久久久|