使用 JDBC for MySQL 从 latin1 字段查询中文值时无法获取正确的值
使用 JDBC for MySQL 从 latin1 字段(varchar 或 char)查询中文值时无法获得正确的值。角色无法改变。以下是测试步骤。对于这种情况是否有可能获得正确的值?我还应该做什么?
1.创建数据库:
CREATE DATABASE TESTDB CHARACTER SET latin1 COLLATE latin1_general_ci;
2.创建表:
CREATE TABLE TB1 (
vname varchar(50) default '',
cname char(50) default ''
) DEFAULT CHARSET=latin1 ;
3.插入数据:
set names latin1;
insert into TB1(vname,cname) values('名字v','名字c');
4.显示字符集:
mysql> show variables like '%set%';
+--------------------------+---------------------------------------------------------+
| Variable_name | Value |
+--------------------------+---------------------------------------------------------+
| auto_increment_offset | 1 |
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | latin1 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | F:\Program Files\MySQL\MySQL Server 5.5\share\charsets\ |
+--------------------------+---------------------------------------------------------+
5.从命令控制台查询时可以得到正确的值:
mysql> select * from tb1;
+-------+-------+
| vname | cname |
+-------+-------+
| 名字v | 名字c |
+-------+-------+
1 row in set (0.00 sec)
6.使用JDBC查询时无法得到正确的值:
jdbc url : jdbc:mysql://192.168.5.74/testdb?characterEncoding=UTF-8
*set names utf8;
select vname,hex(vname),length(vname),char_length(vname) from tb1;
select cname,hex(cname),length(cname),char_length(cname) from tb1;
select vname,cname
,CONVERT(CONVERT(CONVERT(vname USING latin1) USING binary) USING utf8) as c1
,CONVERT(CONVERT(CONVERT(cname USING latin1) USING binary) USING utf8) as c2
from tb1;*
vname hex(vname) length(vname) char_length(vname)
--------------- -------------- ------------- ------------------
??×?v C3FBD7D676 5 5
cname hex(cname) length(cname) char_length(cname)
--------------- -------------- ------------- ------------------
??×?c C3FBD7D663 5 5
vname cname c1 c2
----------------- --------- ------- ------
??×?v ??×?c
It can't get correct value to query Chinese value from latin1 field (varchar or char) Using JDBC for MySQL. The character can't be changed. The following is the test steps. Is it possible to get the correct value for this situation? What's more should I do?
1. create database:
CREATE DATABASE TESTDB CHARACTER SET latin1 COLLATE latin1_general_ci;
2. create table:
CREATE TABLE TB1 (
vname varchar(50) default '',
cname char(50) default ''
) DEFAULT CHARSET=latin1 ;
3. insert data:
set names latin1;
insert into TB1(vname,cname) values('名字v','名字c');
4. show character set:
mysql> show variables like '%set%';
+--------------------------+---------------------------------------------------------+
| Variable_name | Value |
+--------------------------+---------------------------------------------------------+
| auto_increment_offset | 1 |
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | latin1 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | F:\Program Files\MySQL\MySQL Server 5.5\share\charsets\ |
+--------------------------+---------------------------------------------------------+
5. it can get correct value when querying from command console:
mysql> select * from tb1;
+-------+-------+
| vname | cname |
+-------+-------+
| 名字v | 名字c |
+-------+-------+
1 row in set (0.00 sec)
6. can't get the correct valuing when using JDBC to query:
jdbc url : jdbc:mysql://192.168.5.74/testdb?characterEncoding=UTF-8
*set names utf8;
select vname,hex(vname),length(vname),char_length(vname) from tb1;
select cname,hex(cname),length(cname),char_length(cname) from tb1;
select vname,cname
,CONVERT(CONVERT(CONVERT(vname USING latin1) USING binary) USING utf8) as c1
,CONVERT(CONVERT(CONVERT(cname USING latin1) USING binary) USING utf8) as c2
from tb1;*
vname hex(vname) length(vname) char_length(vname)
--------------- -------------- ------------- ------------------
??×?v C3FBD7D676 5 5
cname hex(cname) length(cname) char_length(cname)
--------------- -------------- ------------- ------------------
??×?c C3FBD7D663 5 5
vname cname c1 c2
----------------- --------- ------- ------
??×?v ??×?c
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我解决了。 多字节字符存储到latin1字段时会被转换为字节。它需要转换回插入字符集。以下sql可以做到这一点:
我的插入字符集是gb2312,所以sql应该是:
I resolve it. The multi-bytes characters are converted to bytes when storing in latin1 field. It need to convert back to the inserting charset. The following sql can do this:
My inserting charset is gb2312, so the sql should be :
你应该改变 latin1_general_ci;为 utf8_general_ci 或 utf16_general_ci。问题是CHARSET=latin1不能像汉字一样存储unicode字符。
看看这个:
http://dev.mysql.com/doc/refman /5.0/en/charset-unicode-utf8.html
Latin1字符集只有8位单字节,而中文字符集需要多字节
You should change latin1_general_ci; to utf8_general_ci or utf16_general_ci. The problem is CHARSET=latin1 cannot store unicode characters like Chinese characters.
Check out this:
http://dev.mysql.com/doc/refman/5.0/en/charset-unicode-utf8.html
Latin1 charset is only 8bit single byte while Chinese script require multi-bytes
获取所需字符的另一种更简单的方法是从列中获取字节,然后将它们转换为应用程序内的字符串。
伪代码...
检查 mysql 是否意外或设计地在监视器中显示正确字符串的一个非常有用的方法是使用长度函数。
此外,hex() 函数也是您的朋友:
它将向您显示存储在列中的字节的十六进制值,以便您可以查找这些值(是的,维基百科!)以查看它们是否与以各种不同方式查看的显示值相匹配。
Another, simpler way of getting the desired characters would be to get the bytes from the column and then convert those into a string inside the application.
Pseudocode...
A very useful way to check and see if mysql is showing you the correct string in the monitor by accident or design is to use the length functions.
In addition, the
hex()
function is also your friend:That'll show you the hex values of the bytes that are stored in the column so you can then look those up (yay, Wikipedia!) to see if they match the displayed value in the various different ways of viewing it.