---
title: "终极解决MySql乱码"
author: "sean2012"
date: "2013-05-25T15:07:07Z"
tags: []
categories: ["数据库"]
slug: "mysql"
draft: false
lastmod: "2013-05-25T15:14:07Z"
id: 1876020
url: "https://sean2012.iteye.com/blog/1876020"
---

**MySql乱码问题**

        你是否和我遇到了同样的问题，因为MySql数据库中读取中文信息总是乱码而纠结？下面教你一套终极方法来解决它！

1、将整个工程的字符集设置成“UTF-8”：右击整个工程 ——>快捷菜单“property”——>在<span style="color:red">**Resource**</span>中设置“UTF-8”；

2、将JDBC中的URL也设置成“UTF-8”:“ url= "jdbc:mysql://localhost:3306/HouseManage<span style="color:red">**?useUnicode=true&characterEncoding=utf8"**</span>;”

3、将MySql安装目录下的“my.ini”用记事本打开，

在下添加<span style="color:red">**default-character-set=utf8**</span>
	
在下添加<span style="color:red">**default-character-set=utf8**</span>

在“命令提示符下”（win键+R，输入"cmd"命令打开）,重新启动MySql  

<span style="color:red">**net stop mysql**</span>
**<span style="color:red">net start mysql</span>**


4、创建数据库表时要加一个字符集设置


例如：


> create table student_info
> (
>    id                   int not null auto_increment,
>    dentityId            varchar(20) not null,
>    name                 varchar(20) not null,
>    sex                  varchar(4) not null,
>    term                 varchar(20) not null,
>    major                varchar(50) not null,
>    college              varchar(50) not null,
>    state                int not null default 0,
>    primary key (id)
> )[size=large][color=red]type=MyISAM default charset utf8;[/color][/size]


5、注意,请不要直接要数据库中insert数据，不然还是乱码，而要通过java代码实例来对数据库insert；


好啦！Congratulations！成功啦！