这篇文章是关于如何结束mysql中文乱码的。边肖觉得很实用,所以分享给大家学习。希望你看完这篇文章能有所收获。让我们和边肖一起看看。
不知道担心了多少天的中文,最讨厌瑞典人了——他发明了mysql,并设置了默认设置:latin1。我曾经想过,如果中国人发明了mysql(我的看家狗),呵呵,我们还会有这样的烦恼吗?默认值为gk2312或gbk。如果考虑台湾省的朋友加个big5。只是。但是呢?事实并非如此。除了百度、谷歌甚至雅虎,我别无选择。我尝试了我能找到的每一种方法,很长一段时间,直到今天,我找到了,并且想通了。所以我很渴望和大家分享以下经历。为了说清楚,我想到一个例子:
很简单,就是接受html的两个输入,然后通过jsp处理写入mysql,从数据库返回结果进行显示。
Mysql_jstl.html
超文本标记语言
头
title ch14-MySQL _ jstl . html/title
meta http-equiv=' Content-Type ' Content=' text/html;字符集=GB2312 '
/head
身体
H3在Mysql中存储信息-用JSTL写/h3
表单名称='form '操作='Mysql_jstl.jsp '方法='post '
p姓氏:输入名称=' last _ name '类型=' text' ID=' last _ name'/p
p:输入名称=' first _ name '类型=' text' ID=' first _ name'/p
p
类型=“提交”值=“发送”
类型=“重置”值=“取消”
/p
/form
/body
/html
Mysql_jstl.jsp%@页面内容类型=' text/html;charset=GB2312' %
%@ taglib前缀=' c ' uri=' http://Java . sun.com/JSP/jstl/core ' %
%@ taglib前缀=' SQL ' uri=' http://Java . sun.com/JSP/jstl/SQL ' %
%@ taglib前缀=' fmt ' uri=' http://Java . sun.com/JSP/jstl/fmt ' %
超文本标记语言
头
titleCH14 - Mysql_jstl.jsp/title
/head
身体
H3在Mysql中存储信息-用JSTL写/h3
fmt:requestEncoding值='GB2312' /
c:set var='出生'值='1978/12/11' /
c 3360 set var=s
ex" value="F" />
<c:set var="email" value="aaa@asdf.com" />
<sql:setDataSource driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/sample?useUnicode=true&characterEncoding=UTF-8"
user="root"
password="44444" />
<sql:update>
INSERT INTO employee(employee_id, last_name, first_name, birth, sex, emmail)
VALUES ( ? , ? , ? , ? , ? , ? )
<sql:param value="${employee_id}" />
<sql:param value="${param.last_name}" />
<sql:param value="${param.first_name}" />
<sql:param value="${birth}" />
<sql:param value="${sex}" />
<sql:param value="${email}" />
</sql:update>
<sql:query var="result">
SELECT * FROM employee
</sql:query>
从 employee 取出所有新增的姓名:<br>
<c:forEach items="${result.rows}" var="row" >
新增姓名:<c:out value="${row.last_name}" />
<c:out value="${row.first_name}" /><br>
</c:forEach>
</body>
</html>
接下来就是创建数据库,名字为sample
然后建立一个table: employee,内容如下(其中应该是email,可是我不小心在建数据库打错了,将错就错了):
+-------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+------------------+------+-----+---------+----------------+
| employee_id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| last_name | varchar(20) | YES | | NULL | |
| first_name | varchar(20) | YES | | NULL | |
| birth | date | YES | | NULL | |
| sex | enum('m','f') | YES | | m | |
| emmail | varchar(39) | YES | | NULL | |
+-------------+------------------+------+-----+---------+----------------+
如果只是这样的话,就会出现这样的错误:报告的错误是:sqle=com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long
for column 'last_name' at row 1 从 employee!
有的情况是:| name |
+-------------+
| ?? |
| 54243654321 |
| ?? |
| ?? |
+-------------+出现问号!
前面我们已经说过了,mysql默认的编码是latin1,不是我们所需要的gbk,所以我们要修改成为utf8,因为若要正确显示中文繁、简、日文、韩文 使用utf8,修改方法如下:
ALTER DATABASE sample ####这里修改整个数据库的编码
CHARACTER SET utf8
DEFAULT CHARACTER SET utf8
COLLATE utf8_general_ci
DEFAULT COLLATE utf8_general_ci;
当然了,你也可在在建数据库的时候指定编码,比如:
CREATE DATABASE sample
CHARACTER SET utf8
DEFAULT CHARACTER SET utf8
COLLATE utf8_general_ci
DEFAULT COLLATE utf8_general_ci ;
接下来要做的是打开mysql所在的目录下的my.nin
mysql> show variables like '%character%'; +--------------------------+-------------------------------------------+ | Variable_name | Value | +--------------------------+-------------------------------------------+ | character_set_client | latin1 | | character_set_connection | latin1 | | character_set_database | utf8 | | character_set_results | latin1 | | character_set_server | utf8 | character_set_system | utf8 | | character_sets_dir | C:MySQLMySQL Server 5.0sharecharsets | +--------------------------+-------------------------------------------+ mysql> show variables like '%collation%'; +----------------------+-------------------+ | Variable_name | Value | +----------------------+-------------------+ | collation_connection | latin1_swedish_ci | | collation_database | utf8_general_ci | | collation_server | utf8_general_ci | +----------------------+-------------------+
接着你再看看执行那个Mysql.html 文件:这回你可以看到的是
mysql> select * from employee;
+-------------+-----------+------------+------------+------+--------------+
| employee_id | last_name | first_name | birth | sex | emmail |
+-------------+-----------+------------+------------+------+--------------+
| 12 | ? | ?? | 1978-12-11 | f | aaa@asdf.com |
| 13 | ? | ?? | 1978-12-11 | f | aaa@asdf.com |
| 14 | ? | ?? | 1978-12-11 | f | aaa@asdf.com |
| 15 | ? | ?? | 1978-12-11 | f | aaa@asdf.com |
| 16 | ? | ?? | 1978-12-11 | f | aaa@asdf.com |
| 17 | ? | ?? | 1978-12-11 | f | aaa@asdf.com |
+-------------+-----------+------------+------------+------+--------------+
这当然不是我们希望看到的,我们需要的现实完美正确的中文:
我们还有最后一招:
mysql> SET NAMES 'gbk' ;Query OK, 0 rows affected (0.00 sec)
因为我们需要的是gbk.
看看mysql中的character设置情况:
mysql> SHOW VARIABLES LIKE '%character%' ;
+--------------------------+-------------------------------------------+
| Variable_name | Value |
+--------------------------+-------------------------------------------+
| character_set_client | gbk |
| character_set_connection | gbk |
| character_set_database | utf8 |
| character_set_results | gbk |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | C:MySQLMySQL Server 5.0sharecharsets |
+--------------------------+-------------------------------------------+
7 rows in set (0.00 sec)
mysql> SHOW VARIABLES LIKE '%collation%' ;
+----------------------+-----------------+
| Variable_name | Value |
+----------------------+-----------------+
| collation_connection | gbk_chinese_ci |
| collation_database | utf8_general_ci |
| collation_server | utf8_general_ci |
+----------------------+-----------------+
这个才是我们最终需要的。
在来查看执行完Mysql_jstl.jsp后的数据库中的结果:
mysql> select * from employee;
| 14 | 王 | 彭给 | 1978-12-11 | f | aaa@asdf.com |
| 15 | 田 | 王光 | 1978-12-11 | f | aaa@asdf.com |
| 16 | 息 | 存入 | 1978-12-11 | f | aaa@asdf.com |
| 17 | 往 | 小杯 | 1978-12-11 | f | aaa@asdf.com |
+-------------+-----------+------------+------------+------+--------------+
可以高兴得看到了中文,并且在浏览器中也显示正确。
但是仅仅这样的话,当你重新启动mysql的时候
所有的设置又失效了。
mysql> show variables like '%character%';
+--------------------------+-------------------------------------------+
| Variable_name | Value |
+--------------------------+-------------------------------------------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | utf8 |
| character_set_results | latin1 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | C:MySQLMySQL Server 5.0sharecharsets |
+--------------------------+-------------------------------------------+
mysql> SHOW VARIABLES LIKE '%collation%' ;
+----------------------+-------------------+
| Variable_name | Value |
+----------------------+-------------------+
| collation_connection | latin1_swedish_ci |
| collation_database | utf8_general_ci |
| collation_server | utf8_general_ci |
+----------------------+-------------------+
重新读取又出现乱码:
mysql> select * from employee;
+-------------+-----------+------------+------------+--
| employee_id | last_name | first_name | birth | s
+-------------+-----------+------------+------------+--
| 12 | ? | ?? | 1978-12-11 | f
| 13 | ? | ?? | 1978-12-11 | f
| 14 | ? | ?? | 1978-12-11 | f
| 15 | ? | ?? | 1978-12-11 | f
| 16 | ? | ?? | 1978-12-11 | f
| 17 | ? | ?? | 1978-12-11 | f
+-------------+-----------+------------+------------+--
所以我们需要在客户端设置系统能识别中文的编码gbk并没有保存到my.ini文件中。所以要修改my.ini文件
在[mysql]段加入一下代码改成:default-character-set=gbk 这样设置就得到保存了。
重启就可以了。mysql> show variables like '%character%';
+--------------------------+----------------
| Variable_name | Value
+--------------------------+----------------
| character_set_client | gbk
| character_set_connection | gbk
| character_set_database | utf8
| character_set_results | gbk
| character_set_server | utf8
| character_set_system | utf8
| character_sets_dir | C:MySQLMySQL
+--------------------------+----------------
mysql> SHOW VARIABLES LIKE '%collation%' ;
+----------------------+-----------------+
| Variable_name | Value |
+----------------------+-----------------+
| collation_connection | gbk_chinese_ci |
| collation_database | utf8_general_ci |
| collation_server | utf8_general_ci |
+----------------------+-----------------+
以上就是如何终结mysql中文乱码,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注行业资讯频道。
内容来源网络,如有侵权,联系删除,本文地址:https://www.230890.com/zhan/98517.html