Mysql database encoding is frequently used in practical applications, causing a lot of inconvenience to the actual operation of the Mysql database. The following article describes the solution to the Mysql database encoding problem. We all know that Mysql database 4.1 is a watershed, and 4.1 directly supports Unicode. The following versions do not support it well.
Mysql JDBC Driver 3.0.16 is also a watershed. Version 3.0.16 will take the MySQL database encoding of the database itself 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 MySQL 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
In this way, if the database server uses UTF-8, jdbc will upload the database content to the JVM in UTF-8 format.
However, if a database on the database server uses gbk encoding, the encoding inconsistency of the MySQL database will occur! In this case, you can? CharacterEncoding = GBK is forcibly transmitted to JVM using gbk encoding.
Alternatively, you can convert the encoding of the entire Mysql database server to gbk.
In Mysql, lartin1 is used by default, that is, ISO8859-1 character set encoding. This is an 8-bit encoding that applies to all Western European characters. It is not suitable for Chinese characters.
The best and most common MySQL database encoding format is UTF-8, which is an 8-bit Unicode Character Set. It is space-saving and can effectively represent Chinese characters and other characters for the eight-bit Western European character set.
Therefore, set the Mysql database server to the UTF-8 format and set all Mysql databases to the UTF-8 format. This is the best choice! All languages around the world can be well supported!
However, some common Mysql tools cannot display UTF-8 Chinese characters. For example, Mysql tools and Mysql-Front software. However, Mysql in MyManger and command line can normally display UTF-8 Chinese characters.
If you choose to use gbk, you should convert the encoding formats of the database server and the MySQL database to use gbk. Otherwise, problems may occur.