JSP implements Chinese Input in MySQL (the problem that has been plagued for a long time has finally been solved)

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.