Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of mysql supporting gbk coding

2025-03-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you how mysql supports gbk coding, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Mysql supports gbk encoding: first modify the [my.cnf] file; then specify the gbk character set when creating the database table, the code is [> show cereate table tablename]; finally, modify the jdbc driver.

Mysql supports gbk encoding:

In the mysql database, the latin character set is used, so the Chinese characters cannot be supported normally, and the Chinese characters are displayed as garbled in the database. No. In order for mysql to use Chinese normally, especially when using jsp to connect to mysql, we need to use the character set of gbk, so we need to set mysql to support Chinese effectively:

1. Modify the my.cnf file

The my.cnf file is the configuration file of mysql. We can use the mysql installation directory according to its own template.

Establish

# cp / usr/local/mysql/support-files/my-huge.cnf / etc/my.cnf#vi / etc/my.cnf

Add in the appropriate location in this file

Default-character-set = gbk#### [client] default-character-set = Gbk [mysqld] default-character-set = gbk#

After the modification, save, and then log in using the client

# mysql-u root-p

Enter in the client

> status

If the data displayed appears:

Server characterset: gbkDb characterset: gbkClient characterset: gbkConn. Characterset: gbk

Indicates that the modification was successful.

2. Specify the gbk character set when establishing the database table.

We need to specify the gbk character set when creating the database table

Set up the database:

CREATE DATABASE dbname DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci

Create a data sheet

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

Once established, use the following in the client:

> show cereate table tablename

If the last line shows gbk, it is successful

3. Modify the jdbc driver

The jdbc driver is required for jsp to connect to mysql. When using it, we need to set up 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); / / get the connection

Where dbname is the name of your database, and gbk in url is the character set used.

These are all the contents of mysql's method of supporting gbk coding. Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report