MySQL的中文问题

唉,看到网上这么多的关于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语句中的问题已经解决。如果有网友能够有进一步地突破,还希望不吝指教。谢谢了!~  当然,如果本篇有一些不足之处,也请指出哦!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值