使用 Ver 14.12 Distrib 5.0.27, for pc-linux-gnu (i686) 这个版本的mysql,已经将字符集设置为以下形式:
mysql> show Variables like "%set%";
+--------------------------+-------------------------------------------------------------------+
| Variable_name | Value |
+--------------------------+-------------------------------------------------------------------+
| auto_increment_offset | 1 |
| character_set_client | gb2312 |
| character_set_connection | gb2312 |
| character_set_database | gb2312 |
| character_set_filesystem | binary |
| character_set_results | gb2312 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/local/mysql-standard-5.0.27-linux-i686/share/mysql/charsets/ |
但是使用myeclipse6.0开发,不从页面读取,直接在java文件中写入中文,使用PreparedStatement产生insert语句也会产生乱码:
1.我的数据库连接池设置为jdbc:mysql://192.168.61.111:3306/field?useUnicode=true&characterEncoding=gb2312
2.产生SQL语句如下
String insSql = new String("insert into UserInfo(userid,username,password," +
"email,telephone,userrole) values(?,?,?,?,?,'专业人员')"); PreparedStatement preSQL = conn.prepareStatement(insSql);
userid = new String(userid.getBytes("GB2312"));
preSQL.setString(1,userid);
username = new String(username.getBytes("GB2312"));
preSQL.setString(2, username);
password = new String(password.getBytes("GB2312"));
preSQL.setString(3,password);
email = new String(email.getBytes("GB2312"));
preSQL.setString(4,email);
telephone = new String(telephone.getBytes("GB2312"));
preSQL.setString(5,telephone);
System.out.println("#####"+preSQL);
preSQL.executeUpdate();
打印出来的preSQL就有乱码:
com.mysql.jdbc.PreparedStatement@31d5e2: insert into UserInfo(userid,username,password,email,telephone?userrole) values('bird','bird','123','[email protected]','01082310000','????')
请问怎么解决。
mysql> show Variables like "%set%";
+--------------------------+-------------------------------------------------------------------+
| Variable_name | Value |
+--------------------------+-------------------------------------------------------------------+
| auto_increment_offset | 1 |
| character_set_client | gb2312 |
| character_set_connection | gb2312 |
| character_set_database | gb2312 |
| character_set_filesystem | binary |
| character_set_results | gb2312 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/local/mysql-standard-5.0.27-linux-i686/share/mysql/charsets/ |
但是使用myeclipse6.0开发,不从页面读取,直接在java文件中写入中文,使用PreparedStatement产生insert语句也会产生乱码:
1.我的数据库连接池设置为jdbc:mysql://192.168.61.111:3306/field?useUnicode=true&characterEncoding=gb2312
2.产生SQL语句如下
String insSql = new String("insert into UserInfo(userid,username,password," +
"email,telephone,userrole) values(?,?,?,?,?,'专业人员')"); PreparedStatement preSQL = conn.prepareStatement(insSql);
userid = new String(userid.getBytes("GB2312"));
preSQL.setString(1,userid);
username = new String(username.getBytes("GB2312"));
preSQL.setString(2, username);
password = new String(password.getBytes("GB2312"));
preSQL.setString(3,password);
email = new String(email.getBytes("GB2312"));
preSQL.setString(4,email);
telephone = new String(telephone.getBytes("GB2312"));
preSQL.setString(5,telephone);
System.out.println("#####"+preSQL);
preSQL.executeUpdate();
打印出来的preSQL就有乱码:
com.mysql.jdbc.PreparedStatement@31d5e2: insert into UserInfo(userid,username,password,email,telephone?userrole) values('bird','bird','123','[email protected]','01082310000','????')
请问怎么解决。
mysql_query('set names gbk');
mysql_query("other sql");
mysql_query("SET NAMES UTF8")
但是你已经入库的数据是没有办法改变过来的,
只有新录入的数据才是UTF8型
Context ctx=new InitialContext();
Connection conn=null;
DataSource ds=(DataSource)ctx.lookup("java:comp/env/jdbc/mysql");
conn=ds.getConnection();
之后使用mysql_query("set names 'gb2312'")吗?