创建数据库:CREATE DATABASE IF NOT EXISTS liu_yan_ban2 default charset utf8 COLLATE utf8_general_ci; 


[sql] view plaincopy

  1. CREATE TABLE liuyan_temp(  

  2.   id int(6) NOT NULL auto_increment,  

  3.   url varchar(100) default '#',  

  4.   ip varchar(20) default NULL,  

  5.   email varchar(50) default '#',  

  6.   qq varchar(20) default NULL,  

  7.   sj varchar(30) default NULL,  

  8.   content text,  

  9.   xm varchar(20) default '没有姓名',  

  10.   KEY id (id)  

  11. default charset=utf8;  




然后通过后面的set命令把所有character_set之类的变量全部设置成utf8

变量character_set_client还不能设置成utf8,否则,在mysql>提示符下都插入不了中文字段,都无法运行上面的sql,而必须把第9行改成字母。所以,要设成gb2312

必须把character_set_results设置成gb2312,如果设置成utf8,则显示的还是乱码:


查看字符集设置

mysql>show variables like 'collation_%';
mysql>show variables like 'character_set_%';

通过MySQL命令行修改:
mysql> set character_set_client=utf8;
mysql> set character_set_connection=utf8;
mysql> set character_set_database=utf8;
mysql> set character_set_results=utf8;
mysql> set character_set_server=utf8;
mysql> set character_set_system=utf8;
mysql> set collation_connection=utf8;

mysql> set collation_database=utf8;

mysql> set collation_server=utf8;

连接数据库字符串的写法

String dbUrl = "jdbc:mysql://localhost:3306/db_CityInfo?useUnicode=true&characterEncoding=utf-8"


[java] view plaincopy

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

  2.      String url ="jdbc:mysql://"+dbip+":3306/liu_yan_ban2?user="+use+"&password="+pass+"&useUnicode=true&characterEncoding=gb2312" ;  

  3.      conn= DriverManager.getConnection(url);  




//infoSingle.setInfoTitle(rs.getString("info_title"));
String temp=rs.getString("info_title");
temp=new String(temp.getBytes("iso-8859-1"),"gb2312");
System.out.println("刘利新");
System.out.println(temp);
infoSingle.setInfoTitle(temp);

折腾了一晚上,这才见到点中文