Solutions for garbled JSP access to MySQL Chinese Data
There are a lot of materials on the Internet in this regard, but many of them are either sporadic or long articles starting from the concept of coding. I am not engaged in theory, you only need to use it. Don't talk nonsense. Start with the question.
My development environment is: XP-sp2, tomcat5.0, jdk1.42, mysql4.0.21-NT
When playing with JSP, it was found that Chinese data was stored in MySQL or read from MySQL, which showed garbled and speechless. Baidu searched a lot of information to solve the problem. To sum up.
Solution 1:
When connecting to MySQL (whether reading data from MySQL or retrieving data), specify the encoding method as gb2312. 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 = gb2312 ");
Solution 2:
If method 1 does not work, perform forced encoding conversion on the read String Based on method 1.
The sample code is as follows:
String name = RST. getstring ("name ");
Name = new string (name. getbytes ("ISO-8859-1"), "gb2312 ");
Note: The Code can also be: string name = new string (RST. getstring ("name "). getbytes ("ISO-8859-1"), "gb2312"); where RST is the returned resultset, The ISO-8859-1 is the default MySQL encoding method, the purpose of the code is to convert the ISO-8859-1 encoding to gb2312 encoding mode, this forced conversion, can solve a part of the problem, if combined with method 1, it should be able to solve the Chinese garbled problem.
Solution 3:
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 = GBK
Then restart MySQL "(original address: http://www.blogdriver.com/blog/tb. B? Diaryid = 338894)
My. INI is in C: Windows and opened with editplus. The unmodified code is as follows:
......
[Mysqld]
Basedir = E:/program files/MySQL
# Bind-address = 192.168.0.150
......
[Winmysqladmin]
Server = E:/program files/MySQL/bin/mysqld-nt.exe
......
As mentioned in the original article, modify the configuration Code as follows:
......
[Mysqld]
Basedir = E:/program files/MySQL
Default-character-set = GBK
# Bind-address = 192.168.0.150
......
[Winmysqladmin]
Server = E:/program files/MySQL/bin/mysqld-nt.exe
......
Then restart winmysqladmin.exe.
If there is no method I and II as the basis, I have tried this method. With method 1 and method 2, even if method 3 is not used, I also solve the problem of garbled characters in Chinese reading and writing.
Finally, do not forget to declare in JSP:
Contenttype = "text/html; charset = gb2312"