mysql乱码

保证:

  • mysql配置文件为utf-8
  • utf-8连接数据库
  • 建立数据库和表是utf-8
  • tomcat设置为utf-8
  • 网页是utf-8

做好这几点,相信不会乱码了!

1.修改mysql配置文件为utf-8

sudo vi /etc/mysql/my.cnf 

如果是win系统,请修改my.ini
在对应的client,mysql,mysqld加入对应的语句,不要复制
[]里面的内容,比如复制default-character-set=utf8到
[client]下面。

[client]
default-character-set=utf8 [mysql] default-character-set=utf8 [mysqld] character-set-server = utf8 collation-server = utf8_unicode_ci init-connect='SET NAMES utf8'

登录Mysql,输入

show variables like 'character%'

如果设置正确,输出结果应该是

+--------------------------+---------------------------------------------------------------+
| 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 (x86)\MySQL\MySQL Server 5.5\share\charsets\ |
+--------------------------+---------------------------------------------------------------+
8 rows in set

2.utf-8连接数据库,比如在hibernate中

jdbc_url=jdbc:MySQL://localhost:3306/sshe?useUnicode=true&characterEncoding=UTF-8

3.建立数据库和表是utf-8,比如:

使用utf-8建表

CREATE TABLE days_user ( id int(10) unsigned NOT NULL AUTO_INCREMENT, state int(5) DEFAULT NULL, nickname varchar(20) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL, createtime timestamp NULL DEFAULT NULL, PRIMARY KEY (id) ) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8

使用utf-8建数据库

create database db_name default character set utf8 collate utf8_general_ci; 

4.tomcat设置为utf-8
在tomcat家目录下的conf下面打开server.xml,修改成如下图
mysql乱码_第1张图片
5.确认网页是utf-8

<%@ page language=”Java” contentType=”text/html; charset=utf-8” pageEncoding=”utf-8%>

你可能感兴趣的:(数据库,mysql,乱码)