Get the App
SLTechnology News&Howtos  ›  Database  › 

The method of mysql supporting gbk coding

Shulou Source: shulou.com Published: 2022-06-01 02:28:26 10月22日 Update

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!

Tags: Character character set support data file method encoding database Chinese driver client client time success content article valid one line not much garbled Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Xiaomi Microsoft MySQL Shulou Information