Solution 1:
Encoding during mysql installation,
Check whether my. ini exists.
Copy codeThe Code is as follows: [mysql]
Default-character-set = utf8
[Client]
Default-character-set = utf8
[Mysqld]
Default-character-set = utf8:
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 ();
// Connect to the database
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:
Copy codeThe Code is as follows: <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>