Recently I saw a lot of questions about MySQL's support for UTF-8 encoding, And I just used it myself. I found it and did not write it completely.
I sorted it out for your reference.
In the following server settings test passed
Server Configuration:
Window2000
Tomcat 4.1, 5.19
MySQL 4.1, 5.0
Java language, jdk1.4.2
Database-driven mysql-connector-java-3.0.10-stable-bin.jar
Struts1.1
First look at the prompt before you start
MySQL database 4.1 is a watershed, and 4.1 directly supports Unicode, which is not supported in the following versions;
MySQL JDBC driver 3.0.16 is also a watershed. Version 3.0.16 will take the database encoding and convert it according to the encoding. This method is the same as that of oracle JDBC driver. For example, if your database is GBK encoded, The JDBC driver will convert the strings in the database to Unicode according to GBK and send them to JVM. Therefore, it is particularly important to correctly set the database encoding.
MySQL JDBC driver3.0.16 or lower version is not, it is not so intelligent according to the database encoding to determine how to convert, it is always the default use of ISO8859-1, therefore, you must use characterencoding = GBK to force it to convert the string obtained from the database to Unicode Based on GBK.
Therefore, what database version is used, whether it is 3.x, 4.0.x or 4.1.x, is not important to us. There are two important aspects:
1) set the correct database encoding, MySQL and earlier versions of the character set is always the default ISO8859-1, mysql4.1 will let you choose when installing. If you are going to use UTF-8, you need to specify the UTF-8 when creating the database (you can also change it after the creation, 4.1 or later versions can also separately specify the character set of the table)
2) use JDBC driver 3.0.16 or later versions, then you do not need to write what characterencoding = UTF-8
Start setting:
1. Open the winmysqladmin manager, select my. ini settings, and add the code in the [MySQL] section.
Default-character-set = utf8
Then restart the MySQL service.
Open the winmysqladmin manager and select the variables item to check whether the variable value is as follows:
Character_set_server = utf8
Character_set_system = utf8
Character_set_database = utf8
Character_set_client = utf8
Character_set_connection = utf8
Character_set_results = utf8
Collation_connection = utf8_general_ci
Collation_database = utf8_general_ci
Collation_server = utf8_general_ci
If you cannot restart the MySQL service, the spelling may be incorrect. Check the spelling.
The values of the preceding variables are consistent. Congratulations, the setting is successful.
2. The method for calling the database connection in Java is as follows:
JDBC: mysql: // 192.168.1.87/mytest? Useunicode = true & characterencoding = utf8
If the driver uses JDBC driver later than 3.0.16, then you don't need to write any more characterencoding = UTF-8
3. For more information about submitting data processing on the page, see "Chinese problem" and "struts internationalization problem"-the ultimate solution.
4. After completing the three steps above, your system will be able to run with the UTF-8 coding environment.