Solution 1:
Encoding during mysql installation,
Check whether my. ini exists.
[mysql] default-character-set=utf8 [client] default-character-set=utf8 [mysqld] default-character-set=utf8
When creating a table:
DROP TABLE IF EXISTS `Company`; CREATE TABLE IF NOT EXISTS `Company` ( `Cname` VARCHAR(10) NOT NULL, `Caddr` VARCHAR(100), `Ctel` VARCHAR(16) )ENGINE=InnoDB DEFAULT CHARSET=UTF8;
Jsp header:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
If not, try:
contentType="text/html;charset=gb2312"
Solution 2:
When connecting to mysql (whether reading data from mysql or retrieving data), specify the UTF-8 encoding method. The specific code is as follows:
// Load the mysql-jdbc Driver Class. forName ("com. mysql. jdbc. driver "). newInstance (); // Connection sqlCon = DriverManager. getConnection ("jdbc: mysql: // localhost: 3306/test? User = root & password = 1 & useUnicode = true & characterEncoding = UTF-8 ");
Solution 3:
If method 1 does not work, perform forced encoding conversion on the read String Based on method 1.
String name = request.getParameter("name"); name= new String(name.getBytes("ISO-8859-1"),"utf-8");
Or:
String name =new String( rst.getString("name").getBytes("ISO-8859-1"),"utf-8"));
Solution 4:
This method is the first in some articles. I tried this method first, but it does not seem to work. I am not sure if my operation is incorrect. Post the original text first:
"If the database contains Chinese characters and the default mysql language is not Chinese, add a line in [mysqld] in the configuration file C: winntmy. ini:
Default-character-set = UTF-8
Then restart mysql
Method 5: change it in the web. xml file of your project a or web project:
<filter> <filter-name>UTFEncoding</filter-name> <filter-class>com.founder.chenph.Util.EncodingFilter_UTF8 </filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>UTFEncoding</filter-name> <url-pattern>*.jsp</url-pattern> </filter-mapping> <filter-mapping> <filter-name>UTFEncoding</filter-name> <url-pattern>*.do</url-pattern> </filter-mapping>