統一用一種編碼格式,比如gb2312。 要點如下: 1. 將mysql的默認字符集改爲gb2312
my.ini文件中的default-character-set=gb2312;
2. jsp文件character也要設置爲gb2312
<%@page contentType="text/html;charset=gb2312"%>
3. 在進行數據保存之前進行gb2312到iso8859-1編碼的轉換
例如在vo包中: public void setMsg(String msg){ byte temp []; try { temp = msg.getBytes("iso-8859-1"); this.msg = new String(temp); }catch(Exception e){ System.out.println(e); } }
上面這種方式如果不管用的話可以試試下面這種
String queryString = new String(req.getParameter("queryString").getBytes("iso-8859-1"), "gb2312");
4. 配置文件
例如:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="url"> <!--<value>jdbc:mysql://localhost/myNews</value>--> <value>jdbc:mysql://localhost/myNews?useunicode=true&characterencoding=gb2312</value> </bean>
或如下例:
applicationContext.xml中的數據庫連接必須設置爲
<property>jdbc:mysql://localhost/dbname?useUnicode=true&characterEncoding=gb2312</property>
不應該是 jdbc:mysql://localhost/dbname?useUnicode=true&characterEncoding=gb2312