jdbc:mysql://localhost:3306/oa?useUnicode=true&characterEncoding=UTF-8
如果不能插入中文,需要这样创建数据库
create database dbName DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;
1,设置mysql默认编码
首先查询一下show variables like 'character%';
确定所有的character都设置成utf-8
参考 http://www.diybl.com/course/7_databases/mysql/myxl/20081127/152726.html
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 | /usr/share/mysql/charsets/ | +--------------------------+----------------------------+
2,
a) 设置web容器的编码格式。为你的servlet的doGet或doPost方法开始处加入如下代码: request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); b) 为每个jsp页面指定其编码格式。<%@ page pageEncoding="utf-8"%> c) 在连接数据库用的URL后加入:useUnicode=true;characterEncoding=utf-8 如: url="jdbc:mysql:///db1?useUnicode=true&characterEncoding=utf-8",
如果是xml文件中 url="jdbc:mysql:///db1?useUnicode=true&characterEncoding=utf-8",
3, 如果还出现乱码,则有可能是 表中的字段 的编码不是utf-8;
如果出现乱码的表是 book
mysql> show create table book;
+--------------------------+----------------------------+
| Table | Create Table| +--------------------------+----------------------------+
|book | create table 'book'(
'id' int(10) unsigned NOT NULL AUTO_INCREMENT,
'bookname' varchar(45) CHARACTER SET latin1 NOT NULL,
'source' varchar(45) CHARACTER SET latin1 NOT NULL,
PRIMARY KEY('id')
) ENGINE= MyISAM DEFAULT CHARSET=utf8|
+--------------------------+----------------------------+
如果发现字段的字符集不是UTF8. 则对其进行修改
采用其他客户端工具来设定,例如 Navicat/MySQL Front/PhpMyAdmin
或者采用 ALTER 语法来转换字段的字符集
ALTER TABLE book CHANGE bookname bookname CHAR(20) CHARACTER SET utf8; 我在网上找到很多前2点的资料,但是怎么弄都还是乱码,最后才发现原来是字段的原因,折腾死了都。参考资料: mysql字符集 FAQ http://imysql.cn/taxonomy/term/24 设置mysql默认编码 http://www.diybl.com/course/7_databases/mysql/myxl/20081127/152726.html 用UTF-8完全解决JSP MYSQL多国语言文字编码问题 http://www.360doc.com/content/070428/10/16749_471035.html 文章出处:DIY部落(http://www.diybl.com/course/7_databases/mysql/Mysqljs/20090914/175065.html) |