问题:
MySql在命令行(Client)模式下执行查询时,中文显示乱码,执行中文插入时无法插入
解决命令行中文乱码问题如下:
对my.ini文件作如下修改
[client]
default-character-set=latin1
port=3306
[mysql]
default-character-set=latin1
[mysqld]
default-character-set=utf8
# The TCP/IP Port the MySQL Server will listen on
port=3306
# The default character set that will be used when a new schema or table is
# created and no character set is defined
default-character-set=utf8
laint1编码也可以是GBK编码
修改完成后使用如下命令查看mysql服务器编码;
mysql> show variables like 'character\_set\_%';
+--------------------------+--------+
| Variable_name | Value |
+--------------------------+--------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | latin1 |
| character_set_server | utf8 |
| character_set_system | utf8 |
+--------------------------+--------+
命令行乱码解决后,JDBC驱动读取MySql数据库时产生的乱码可以使用编码转换
用(new String(string.getBytes("ISO-8859-1"),"gb2312")将读取的中文转换后,显示正常。
如果将以上设置的latin1编码设置为utf8时,JDBCdq驱动读取MySql中文时不需要编码转换显示也正常
MYSQL在命令行终端下中文显示正常,但在JSP调用显出乱码!
用JSP查询显示乱码,用(new String(string.getBytes("ISO-8859-1"),"gb2312")还是显乱码
用<%@ page contentType="text/html; charset=GB2312;pageEncoding="gb2312"%>还是显乱码
数据库编码类型:UTF-8,GBK,GB2312,LATIN1,ISO-8859-1
MYSQL字符编码的相关设置:
默认的字符是LATIN1,如下:
mysql> show variables like 'character\_set\_%';
+--------------------------+--------+
| Variable_name | Value |
+--------------------------+--------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | latin1|
| character_set_filesystem | binary |
| character_set_results | latin1 |
| character_set_server | latin1|
| character_set_system | latin1 |
+--------------------------+--------+
如果在 /etc/mysql/my.cnf里
[mysqld]下添加default-character-set=utf8
比如:
#
# * Basic Settings
#
user = mysql
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
port = 3306
basedir = /usr
datadir = /var/lib/mysql
tmpdir = /tmp
language = /usr/share/mysql/english
#character_set_client=utf8
#character_set_server=utf8
#character_set_connection=utf8
#character_set_database=utf8
#character_set_results=utf8
default-character-set=utf8
重启MYSQL,发现字符变化了,变化的是 character_set_server跟system
mysql> show variables like 'character\_set\_%';
+--------------------------+--------+
| Variable_name | Value |
+--------------------------+--------+
| 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 |
+--------------------------+--------+
这时候向数据库插入中文字符,JSP显的还是乱码
如果在MYSQL命令行中运行命令:set names utf8;
再插入中文字符,OK!现在JSP 中中文显示正常!!
set names utf8;命令相当于下面三条命令
set character_set_client=utf8;
set character_set_connection=utf8;
set character_set_results=utf8;
如图:
mysql> show variables like 'character\_set\_%';
+--------------------------+--------+
| Variable_name | Value |
+--------------------------+--------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
+--------------------------+--------+
character_set_server:这是设置服务器使用的字符集
character_set_client :这是设置客户端发送查询使用的字符集
character_set_connection :这是设置服务器需要将收到的查询串转换成的字符集
character_set_results :这是设置服务器要将结果数据转换到的字符集,转换后才发送给客户端
小总结:1.这样每次进入MYSQL终端运行set names utf8;可以搞定乱码,可是感觉比较烦,因为只对当前的会话有效,下次进入MYSQL仍然要运行这个命令。想在MY。CNF里加上
character_set_client=utf8
#character_set_server=utf8
character_set_connection=utf8
character_set_database=utf8
character_set_results=utf8
但是,这样不行,加了后MYSQL根本启动不起来,只能设置character_set_server=utf8这个,别的设置不了,如果 有哪个兄弟搞定了,麻烦告诉我一声!
#必须在[mysqld]这段
[mysqld]
default-character-set=utf8
jdbc:mysql://localhost/test
实际上server端字符集不是utf8的话,加了也没用(jdbc:mysql://localhost/test&characterEncoding=UTF-8或者GBK)
可能还会报错;是utf8的话,加不加都行
windows平台:
mysql -u root -p --default-character-set=gbk
linux平台:
mysql -u root -p --default-character-set=utf8 (这个相当于进入MYSQL后运行set names utf8;)
否则进去select中文是乱码,insert中文也不能正确保存
4. <%@ page pageEncoding="UTF-8"%>这句是制定客户端的浏览器以什么字符查看本页面,
相当于FIREFOX浏览器里的“查看”--》“字符编码”--》“UTF-8”,当然如果制定. <%@ page pageEncoding=“GB2312"%>那么查看本页面的浏览器里的字符编码变为GB2312