页面传值中有中文的时候,可以使用filter来搞掂,可是好容易System.out.println可以出中文了,插入到Mysql里,还是乱码。(超,你的免费空间可能也是这个问题)
怎么办呢,做到以下几点:
1。创建数据库的时候:
CREATEDATABASE `Db`
CHARACTERSET'utf8 '
COLLATE 'utf8_general_ci ';
2.建表的时候:
CREATETABLE `TableA` (
`ID` varchar(40) NOTNULLdefault'',
`UserID` varchar(40) NOTNULLdefault'', )
ENGINE=InnoDB DEFAULT CHARSET=utf8;
3.设置URL的时候:
jdbc:mysql://localhost:3306/database?useUnicode=true&characterEncoding=UTF-8
方法二:
MySQL默认编码是latin1
mysql> show variables like 'character%';
+--------------------------+--------------------------+
| Variable_name | Value |
+--------------------------+--------------------------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | latin1 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | D:\MySQL\share\charsets\ |
+--------------------------+--------------------------+
创建数据表并插入数据
mysql> use test;
mysql> create table messages (
-> id int(4) unsigned auto_increment primary key,
-> message varchar(50) not null
-> ) engine=myisam default charset=utf8;
mysql> insert into messages (message) values ("测试MySQL中文显示");
mysql> select * from messages;
+----+-------------------+
| id | message |
+----+-------------------+
| 1 | 测试MySQL中文显示 |
+----+-------------------+
编写程序(Java)
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class JDBCTest {
public static void main(String[] args) {
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/test";
String user = "root";
String password = "root";
try {
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, user, password);
Statement stmt = conn.createStatement();
stmt.executeUpdate("insert into messages (message) values ('测试MySQL编码')");
ResultSet rs = stmt.executeQuery("select * from messages");
while (rs.next()) {
int id = rs.getInt("id");
String message = rs.getString("message");
System.out.println(id + " " + message);
}
rs.close();
stmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
程序输出
1 ????MySQL????????
2 ??MySQL??
我们看到,尽管使用数据库时我们能够正常的添加和显示中文,但是在使用程序连接数据库时并不能够正常显示中文,为此我们需要修改MySQL的默认编码,编辑my.ini(MySQL配置文件)文件对编码进行修改
设置MySQL的默认字符集为utf8,找到客户端配置[client]在下面添加。
default-character-set=utf8
找到服务器配置[mysqld]在下面添加
default-character-set=utf8
设定MySQL数据库以utf8编码运行,连接MySQL数据库时使用utf8编码
停止和重新启动MySQL
net stop mysql
net start mysql
重新连接数据库,查看编码,数据表内容
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 | D:\MySQL\share\charsets\ |
+--------------------------+--------------------------+
mysql> use test;
mysql> select * from messages;
+----+-------------------------------+
| id | message |
+----+-------------------------------+
| 1 | 虏芒脢脭MySQL脰脨脦脛脧脭脢戮 |
| 2 | ??MySQL?? |
+----+-------------------------------+
这里显示依然是乱码主要是因为之前使用的编码不同造成的,重新运行之前写的程序:java JDBCTest
1 ????MySQL????????
2 ??MySQL??
3 测试MySQL编码
从第三条记录我们可以看到现在程序连接数据库时可以正常的添加和显示中文了
mysql> select * from messages;
+----+-------------------------------+
| id | message |
+----+-------------------------------+
| 1 | 虏芒脢脭MySQL脰脨脦脛脧脭脢戮 |
| 2 | ??MySQL?? |
| 3 | 娴嬭瘯MySQL缂栫爜 |
+----+-------------------------------+