Home > Database > Mysql Tutorial > body text

How does mysql support gbk encoding

coldplay.xixi
Release: 2020-10-19 14:33:25
Original
4361 people have browsed it

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
Copy after login

Add

default-character-set = gbk
########################
[client]
default-character-set = gbk
[mysqld]
default-character-set = gbk
#########################
Copy after login

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

#mysql -u root -p
Copy after login

Enter the

>status;
Copy after login

displayed in the client If:

Server characterset:   gbk
Db    characterset:   gbk
Client characterset:   gbk
Conn. characterset:   gbk
Copy after login

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
Copy after login

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
Copy after login

After creation, use it in the client:

>show cereate table tablename;
Copy after login

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);//取得连接
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!