The Latin character set is used in the MySQL database. Therefore, Chinese characters cannot be normally supported. Chinese characters are garbled "?" in the database. .
To enable MySQL to use Chinese characters normally, especially when using JSP to connect to MySQL, we need to use the GBK character set. Therefore, we need to set MYSQL as follows, to effectively support Chinese characters:
1. Modify the my. CNF file my. CNF file to be the MySQL configuration file,
You can create a MySQL installation directory based on its built-in template.
# Cp/usr/local/MySQL/support-files/my-huge.cnf/etc/My. CNF # vi/etc/My. CNF Add the corresponding location in this file
Default-character-set = GBK ########################
[Client] default-character-set = GBK [mysqld] default-character-set = GBK
#########################
After the modification is complete, save the modification and log on to the client. # mysql-u root-P: enter> Status in the client;
If:
Server characterset: GBK
DB characterset: GBK
Client characterset: GBK
Conn. characterset: GBK
The modification is successful.
2. When creating a database table, specify the GBK character set. When creating a database table, specify the GBK character set to create a database:
Create Database dbname default Character Set GBK collate gbk_chinese_ci create a data table
Create Table tablename (ID int (10) unsigned not null auto_increment, name varchar (15) not null default '', primary key (ID )) type = MyISAM default Character Set GBK,
Use:> show cereate table tablename on the client. If GBK is displayed on the last line, the result is successful.
3. Modify the JDBC driver JSP to connect to MySQL using the JDBC driver,
We need to set the character set.
String user = "root"; string Password = "123"; string url = "JDBC: mysql: // localhost: 3306/dbname?Useunicode = true & characterencoding = GBK"Class. fornname (" com. MySQL. JDBC. Driver "); // load the driver class; connection con = drivermanager. getconnection (URL, user, password );//
This step is critical !!!!!
Get the connection where dbname is your database name, And GBK in the URL is the character set used
After completing the preceding three steps, MySQL can support GBK Chinese.