唉,看到網上這么多的關于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兩個連接屬性,下面介紹一點點:
useUnicode | Should 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' |
characterEncoding | If '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語句中的問題已經解決。如果有網友能夠有進一步地突破,還希望不吝指教。謝謝了!~? 當然,如果本篇有一些不足之處,也請指出哦!