There are many information on the Internet, but many either a few stars, or the long story begins with the concept of the coding, I am not engaged in theory, it will use it. Do not say nonsense, start the topic.
My development environment is: XP-SP2, Tomcat5.0, JDK1.42, MySQL4.0.21-NT
When playing JSP, JSP is found to store Chinese data into mysql or read it from MySQL. It is garbled, speechless, and sear with Baidu, solve the problem. Now summary.
Solution 1:
When connecting MySQL (whether oriented from MySQL reading or taking data), the specified encoding method is GB2312, and the specific code is as follows.
// Load the mysql-jdbc driver class.Forname ("com.mysql.jdbc.driver). NewInstance (); // Connect database connection sqlcon = drivermanager.getConnection (" JDBC: mysql: // localhost: 3306 / test? User = root & password = 1 & useunicode = true & characterencoding = GB2312 ");
Solution 2:
If the method does not work, the read string is converted to the read string on the basis of the method.
The code example is, for example:
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"). Gettes ("ISO-8859-1"), "GB2312"); where RST is RESUTSET, ISO-8859- 1 For the MySQL default encoding method, the purpose of the code is to convert the encoding of ISO-8859-1 to GB2312 encoding, so that force conversion can solve some problems, if combined method one, you should solve Chinese garbled problems.
Solution 3:
This method is pushed in some articles, I first tried this method, but it seems that it will not. Here is still, I don't know if I have an error. Still first, "" If there is Chinese in the database, the mysql default language is not Chinese, add a line 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 XP system, so my.ini is under C: Windows, open with Editplus, the code that has not been modified 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 ... According to the original text, the modification configuration code is as follows: .... .. -nt.exe ...... Then restart Winmysqladmin.exe If there is no way to one and two, I have tried it in this way. With the method of the method, even if you don't have to use the method, I also solve the problem of garbled code written in Chinese.
Finally, don't forget to declare in JSP: contentType = "text / html; charset = GB2312"