react 前端框架如何驱动企业数字化转型与创新发展
351
2023-12-24
本篇文章给大家分享的是有关如何终结mysql中文乱码,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
我自己都不知道为了中文问题烦恼了多少个日子,最恨瑞典人了-谁叫他发明了mysql,还设置了默认设置:latin1.曾经也在想假如中国人发明了 mysql(my see狗),呵呵,那么我们还有这样烦恼吗?默认的就是gk2312 或者gbk.倘若考虑到台湾朋友加一个big5.就得了。可是呢?事实不是这样的,没有办法,自己只好baidu一下,google一下,甚至yahoo 一下。能找到的方法都试过了,好久了,直到今天让我找到了,想明白了。所以急切和大家分享以下心得。
为了说的明白一些,我觉个例子:
很简单,就是从html中接受两个输入,然后由jsp处理写到mysql且从数据库返回这个结果显示出来。
Mysql_jstl.html
CH14 - Mysql_jstl.html
姓:
名:
</html>Mysql_jstl.jsp
<%@ page contentType="text/html;charset=GB2312" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
CH14 - Mysql_jstl.jsp
url="jdbc:mysql://localhost:3306/sample?useUnicode=true&characterEncoding=UTF-8"
user="root"
password="44444" />
INSERT INTO employee(employee_id, last_name, first_name, birth, sex, emmail)
VALUES ( ? , ? , ? , ? , ? , ? )
SELECT * FROM employee
从 employee 取出所有新增的姓名:
新增姓名:
</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 utf8COLLATE utf8_general_ci
DEFAULT COLLATE utf8_general_ci ;
接下来要做的是打开mysql所在的目录下的my.ninmysql> 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中文乱码,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注行业资讯频道。
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~