A.mysql设置
1.service mysql stop
2.sudo vim /etc/mysql/my.cnf
在[mysqld]中添加下面两行
character_set_server = utf8
init_connect = 'SET NAMES utf8'
3.service mysql start
B.建库和建表代码加上字符集编码
create database `db_name` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
create table `tb_name` ( id varchar(40) primary key ...... ) engine=innodb default character set=utf8;
或者修改已经存在的库和表的字符集编码
ALTER DATABASE `db_name` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
ALTER TABLE `tb_name` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
C.调用的url加上字符集编码
url = jdbc:mysql:///`db_name`?useUnicode=true&characterEncoding=utf8
如果用在c3p0框架的xml配置文件中,要对&转义
?useUnicode=true&characterEncoding=UTF8