创建数据库:CREATE DATABASE IF NOT EXISTS liu_yan_ban2 default charset utf8 COLLATE utf8_general_ci;
[sql] view plaincopy
-
CREATE TABLE liuyan_temp(
-
id int(6) NOT NULL auto_increment,
-
url varchar(100) default '#',
-
ip varchar(20) default NULL,
-
email varchar(50) default '#',
-
qq varchar(20) default NULL,
-
sj varchar(30) default NULL,
-
content text,
-
xm varchar(20) default '没有姓名',
-
KEY id (id)
-
) 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
-
Class.forName("com.mysql.jdbc.Driver").newInstance();
-
String url ="jdbc:mysql://"+dbip+":3306/liu_yan_ban2?user="+use+"&password="+pass+"&useUnicode=true&characterEncoding=gb2312" ;
-
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);
折腾了一晚上,这才见到点中文