Home  >  Article  >  Database  >  解决mysql中文乱码(三个地方要注意):_MySQL

解决mysql中文乱码(三个地方要注意):_MySQL

WBOY
WBOYOriginal
2016-05-27 14:12:131038browse

bitsCN.com 1.查看mysql字符集

SHOW VARIABLES LIKE'character_set_%';

/

设置字符集命令:SETcharacter_set_client='utf8';

2.新建的数据库和表的字符编码都是utf8

3.代码中连接数据库的字符编码

Class.forName("com.mysql.jdbc.Driver");

conn= DriverManager.getConnection("jdbc:mysql://192.168.99.122:3306/mtime123?user=root&password=root123&useUnicode=true&characterEncoding=utf-8&autoReconnect=true&failOverReadOnly=false");

其实只要做到字符编码统一并且支持中文就可以了,但是有时候我们发现尽管我们做到了以上三点还是会乱码,在程序中会报错某个字段的值是一串二进制编码,原因是该字段没有统一编码,不是utf8。。。。

将数据库结构导出成sql文本,然后打开检查里面的数据库,表和每个字段是否是utf8(只要做到统一编码就行),如果不是就改过来。

DROP TABLE IF EXISTS `award`;CREATE TABLE `award` (  `id` bigint(32) unsigned NOT NULL AUTO_INCREMENT,  `award_content` longtext CHARACTER SET utf8,  `movie_id` bigint(20) DEFAULT NULL,  PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8 CHECKSUM=1 DELAY_KEY_WRITE=1 ROW_FORMAT=DYNAMIC;
bitsCN.com
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn