Home  >  Article  >  Database  >  How does mysql support gbk encoding

How does mysql support gbk encoding

coldplay.xixi
coldplay.xixiOriginal
2020-10-19 14:33:254365browse

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

How does mysql support gbk encoding

Mysql supports gbk encoding method:

In the mysql database, the latin character set is used, so Chinese characters cannot be supported normally, and Chinese characters are displayed as garbled "?" characters in the database. In order for mysql to use Chinese normally, especially when using jsp to connect to mysql, we need to use the gbk character set, so we need to make the following settings for mysql so that it can effectively support Chinese:

1. Modify the my.cnf file

The my.cnf file is the configuration file of mysql. We can

create it from the mysql installation directory based on its own template

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

Add

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

to the corresponding position in this file. After modification, save, and then use the client to log in

#mysql -u root -p

Enter the

>status;

displayed in the client If:

Server characterset:   gbk
Db    characterset:   gbk
Client characterset:   gbk
Conn. characterset:   gbk

appears in the data, it means the modification is successful.

2. Specify the gbk character set when creating the library table

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

Establish the 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

After creation, use it in the client:

>show cereate table tablename;

If the last line displays gbk, it means success

3. Modify the jdbc driver

jsp needs to use the jdbc driver to connect to mysql. When using it, 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");//装载驱动类;
Connection con=DriverManager.getConnection(url,user,password);//取得连接

where dbname is the name of your database, url The gbk in is the character set used

More related free learning recommendations: mysql tutorial(Video )

The above is the detailed content of How does mysql support gbk encoding. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn