Solution to garbled characters when JSP reads MYSQL database

Source: Internet
Author: User
Problem:

MYSQL displays normal Chinese characters on the command line terminal, but garbled characters are displayed in JSP calls!

Display garbled characters with JSP query, with (new String (string. getBytes ("ISO-8859-1"), "gb2312") or show garbled

Use <% @ page contentType = "text/html; charset = GB2312; pageEncoding =" gb2312 "%> to display garbled characters.

The above encoding with UTF-8, GBK, GB2312, LATIN1, ISO-8859-1 and Other encoding tried, the result is still garbled, do not know why ???

Answer:

After half a day, I finally got it done.
Or MYSQL character encoding Problems

The default character is LATIN1, as follows:
Mysql> show variables like 'character/_ set/_ % ';
+ -------------------------- + -------- +
| Variable_name | Value |
+ -------------------------- + -------- +
| Character_set_client | latin1 |
| Character_set_connection | latin1 |
| Character_set_database | latin1 |
| Character_set_filesystem | binary |
| Character_set_results | latin1 |
| Character_set_server | latin1 |
| Character_set_system | latin1 |
+ -------------------------- + -------- +


In/etc/mysql/my. cnf

Add default-character-set = utf8 under [mysqld]

For example:
#
# * Basic Settings
#
User = mysql
Pid-file =/var/run/mysqld. pid
Socket =/var/run/mysqld. sock
Port = 3306
Basedir =/usr
Datadir =/var/lib/mysql
Tmpdir =/tmp
Language =/usr/share/mysql/english
# Character_set_client = utf8
# Character_set_server = utf8
# Character_set_connection = utf8
# Character_set_database = utf8
# Character_set_results = utf8

Default-character-set = utf8


Restart MYSQL and check that the characters have changed. The changed characters are character_set_server and system.
Mysql> show variables like 'character/_ set/_ % ';
+ -------------------------- + -------- +
| Variable_name | Value |
+ -------------------------- + -------- +
| Character_set_client | latin1 |
| Character_set_connection | latin1 |
| Character_set_database | latin1 |
| Character_set_filesystem | binary |
| Character_set_results | latin1 |
| Character_set_server | utf8 |
| Character_set_system | utf8 |

+ -------------------------- + -------- +


At this time, Chinese characters are inserted into the database. The JSP still displays garbled characters.

If you run the command: set names utf8;
Insert Chinese characters again. OK! Chinese characters in JSP are displayed normally !!

Set names utf8; the command is equivalent to the following three commands
Set character_set_client = utf8;

Set character_set_connection = utf8;

Set character_set_results = utf8;



Mysql> show variables like 'character/_ set/_ % ';
+ -------------------------- + -------- +
| Variable_name | Value |
+ -------------------------- + -------- +
| Character_set_client | utf8 |
| Character_set_connection | utf8 |
| Character_set_database | latin1 |
| Character_set_filesystem | binary |
| Character_set_results | utf8 |
| Character_set_server | utf8 |
| Character_set_system | utf8 |
+ -------------------------- + -------- +


Character_set_server: This is the character set used by the server.
Character_set_client: Set the character set used by the client for sending and querying.
Character_set_connection: This is the character set for the server to convert the received query string
Character_set_results: This is the character set for the server to convert the result data to. It is sent to the client only after conversion.


Summary: 1. in this way, you can run set names utf8 on the MYSQL terminal each time. garbled characters can be handled, but this is annoying because it is only valid for the current session and will still run this command the next time you enter MYSQL. In MY. Add


Character_set_client = utf8
# Character_set_server = utf8
Character_set_connection = utf8
Character_set_database = utf8
Character_set_results = utf8

However, this is not the case. After MYSQL is added, it cannot be started at all. You can only set character_set_server = utf8. Other settings cannot be set. If any of the boys is done, please let me know!

2 .. MysqlYou can set the character set to utf8 during installation or in my. cnf. Mysql)
# It must be in the [mysqld] Section
[Mysqld]
Default-character-set = utf82.jdbc connection no need to add characterEncoding, will automatically detect
Jdbc: Mysql: // Localhost/test
In fact, if the server character set is not utf8, it is useless to add it (jdbc: Mysql: // Localhost/test & characterEncoding = UTF-8 or GBK)
An error may be reported. If it is utf8, you can add or not. MysqlClient remember to add -- default-character-set
Windows:
Mysql-U root-p -- default-character-set = gbk
Linux platform:
Mysql-U root-p -- default-character-set = utf8 (this is equivalent to running set names utf8 after entering MYSQL ;)
Otherwise, the select Chinese characters are garbled, And the insert Chinese characters cannot be properly saved.



4. <% @ page pageEncoding = "UTF-8" %> specifies the characters in the browser of the client to view the page,
Similar to the FIREFOX browser "View" -- "" character encoding "--" "UTF-8", of course if the formulation. <% @ page pageEncoding = "GB2312" %> the character encoding in the browser on this page is changed to GB2312.

Related Article

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.