将mysql数据库设置成utf8编码时插入查询中文乱码终极解决方案

①这个问题是我偶然间发现的

org.apache.jasper.JasperException: /admins/userManagement.jsp(1,1)
Page directive: illegal to have multiple occurrences of
contentType with different values (old: text/html;charset=utf8, new: text/html;charset=gbk)

jsp的这个里面的 contentType 属性可能与相应的 html 的 contentType 有关联
<%@ page language="java" import="java.util.*" contentType="text/html;charset=utf8" pageEncoding="utf8"%>

html的如下
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>后台管理系统</title>
<meta http-equiv="Content-Type" content="text/html; charset=gbk" />
<link href="backSystem.css" rel="stylesheet" type="text/css" />
<script src="js/backSystem.js" type="text/javascript" ></script>
</head>...

两者必须相一致!!不能一个为 utf8,一个为 gbk,不然话就会报出上面那个错误~


②在安装mysql数据库的时候如果不小心选择了默认的 latin1,再去修改 my.ini 文件的时候
可能出现有些东西改不过来的情况
一般情况下,如果安装的时候选择 gbk的话,则将 myeclipse 项目,各个jsp页面,过滤器都设置成 gbk
这种情况下出现乱码的几率不会太大,这是经过我实践检验的
以前在使用gbk编码的mysql数据库时,基本上很少遇到现在这般的 乱码问题~~

如果将在安装mysql数据库的时候选择 utf8 的话,则后面有很多注意的地方,不然话将导致很多麻烦的问题
诸如中文插不进mysql数据库,或者中文插入mysql数据库再取出来的时候变成了乱码~

如果在安装时选择了 utf8,则在黑屏下执行“ SHOW VARIABLES LIKE 'character_set%'; ”
命令,一般会得到以下结果:
+--------------------------+---------------------------------------------------------+
| Variable_name | Value
+--------------------------+---------------------------------------------------------+
| character_set_client | utf8
|
| character_set_connection | utf8
|
| character_set_database | utf8
|
| character_set_filesystem | binary
|
| character_set_results | utf8
|
| character_set_server | utf8
|
| character_set_system | utf8
|
| character_sets_dir | C:/Program Files/MySQL/MySQL Server 5.1/share/charsets/ |
+--------------------------+---------------------------------------------------------+
8 rows in set (0.00 sec)

然而这个情况下,插入中文是插入不了的!!
mysql> show tables;
+------------------+
| Tables_in_estore |
+------------------+
| books |
| test |
| user |
+------------------+
3 rows in set (0.00 sec)

mysql> insert into test values(2,'养肠胃');
ERROR 1366 (HY000): Incorrect string value: '/xD1/xF8/xB3/xA6/xCE/xB8' for colum
n 'name' at row 1

怎么办呢??看下面的解决方案~
mysql> set names 'gbk';
Query OK, 0 rows affected (0.00 sec)

mysql> desc test;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(50) | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)

mysql> insert into test values(3,'朱昱');
Query OK, 1 row affected (0.00 sec)


继续往下走
mysql> SHOW VARIABLES LIKE 'character_set%';
+--------------------------+---------------------------------------------------------+
| Variable_name | Value
|
+--------------------------+---------------------------------------------------------+
| character_set_client | gbk
|
| character_set_connection | gbk
|
| character_set_database | utf8
|
| character_set_filesystem | binary
|
| character_set_results | gbk
|
| character_set_server | utf8
|
| character_set_system | utf8
|
| character_sets_dir | C:/Program Files/MySQL/MySQL Server 5.1/share/charsets/ |
+--------------------------+---------------------------------------------------------+
8 rows in set (0.00 sec)

可以看到这时候 character_set_client 、character_set_connection 、character_set_results
的值都变成 gbk 了,也就是说 set names 'gbk' 修改了这3个变量的值!!!


然后:
mysql> set character_set_results='utf8';
Query OK, 0 rows affected (0.00 sec)

mysql> select * from test;
+----+-----------+
| id | name |
+----+-----------+
| 1 | 鎵暱浼? |
| 3 | 鏈辨槺 |
+----+-----------+
2 rows in set (0.00 sec)

还原:
mysql> set character_set_results='gbk';
Query OK, 0 rows affected (0.00 sec)

mysql> select * from test;
+----+--------+
| id | name |
+----+--------+
| 1 | 扬长伟 |
| 3 | 朱昱 |
+----+--------+
2 rows in set (0.00 sec)


继续试验!!
mysql> set names 'latin1';
Query OK, 0 rows affected (0.00 sec)

mysql> select * from test;
+----+------+
| id | name |
+----+------+
| 1 | ??? |
| 3 | ?? |
+----+------+
2 rows in set (0.00 sec)

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
|
| character_sets_dir | C:/Program Files/MySQL/MySQL Server 5.1/share/charsets/ |
+--------------------------+---------------------------------------------------------+
8 rows in set (0.00 sec)

mysql> insert into test values (4,'打发');
Query OK, 1 row affected (0.00 sec)

mysql> select * from test;
+----+------+
| id | name |
+----+------+
| 1 | ??? |
| 3 | ?? |
| 4 | 打发 |
+----+------+
3 rows in set (0.01 sec)

③我曾经在这个项目上遇到过这样一个问题!!
整个 E-StorePro 项目采用的编码格式为 UTF-8 ,该项目里面的每个 JSP 页面涉及到编码格式的设置都为 UTF-8
该项目里面的每个 html 页面涉及到编码格式的也都设置为 UTF-8 !!
有一天我不死心(后台JSP页面采用 了嵌套JSp页面的模式,现在看来感觉真是乱糟糟的,当初还为此自鸣得意节省代码),
把某个JSP页面的编码格式改成了 gbk,结果就导致有时候进入到后台的时候报错,而有时候却正常地不得了~

一般情况下,设置为如下这样就差不多了~
| character_set_client | gbk
|
| character_set_connection | gbk
|
| character_set_database | utf8
|
| character_set_filesystem | binary
|
| character_set_results | gbk
|
| character_set_server | utf8
|
| character_set_system | utf8

整个项目的编码格式建议为utf-8,整个项目的所有jsp,html页面编码选项都设置成utf-8
过滤器filter貌似无所谓,我filter是像下面这么写得,感觉没什么用~
request.setCharacterEncoding("utf8");
response.setCharacterEncoding("utf8");

④数据库连接字符串带还是不带 “&useUnicode=true&characterEncoding=UTF-8” 呢?
如果采用3所述的设置的话,带上则会出现乱码,不带竟然也出现了乱码!!!蛋疼啊!!
回想到底是什么使我能够在mysql数据库中插入正常地中文??
对了,难道是因为添加书籍数据的那几个页面将所有与编码相关的变量都设置成了“gbk”???
我初步猜测应该是这样的,有待我进一步实验!!!!
经过我的反复测试,妈的果然不出我所料!!!!!!
经过进一步实验,发现无论是 set names 'gbk'、
set names 'utf8' 还是
set names 'latin1'
都能正常地插入和查询出中文,最最关键的其实不在于数据库
而在于 jsp、html 页面的编码是否为 GBK !!!这是最终的结论!!

你可能感兴趣的:(mysql)