MySQL Write Chinese garbled

Source: Internet
Author: User

Garbled is the problem of the program ape, the following is a general introduction of several common situations. common in the page, foreground to background value, write to the database three aspects.

1. garbled pages

The page is relatively easy to solve, often in the corresponding JSP page or HTML set the relevant character set for the page. As

<% @page language= "java" contenttype= "text/html; Charset=utf-8 "    pageencoding=" Utf-8 "%>


2. Pass-through value garbled

In the process of passing the value, it is also garbled to appear frequently. First of all, what is the scenario, usually a common scenario is as follows

configures the specified Filter


<!--Configure the request filter, the encoding format is set to UTF-8, avoid Chinese garbled-    <filter>      <filter-name>springutf8encoding</ Filter-name>      <filter-class>org.springframework.web.filter.characterencodingfilter</ filter-class>       <init-param>          <param-name>encoding</param-name>          <param-value >UTF-8</param-value>       </init-param>       <init-param>          <param-name> forceencoding</param-name>          <param-value>true</param-value>       </init-param>    </filter>



SetRequestCharacter Set

are often passed from the foreground to the corresponding Controller or a Action after the garbled, tell me the general idea is to print first Request the default character set itself

I:system.out.println (Request.getcharacterencoding ());


Then, if you print a character set that is not required, set the appropriate character sets

Ii:request.setCharacterEncoding ("UTF-8");


of course, a situation may not be solved by this time with one of the following


String str=newstring ((Request.getparameter ("Bigquestiontypename")). GetBytes ("Iso-8859-1"), "Utf-8")


3. stored in the database garbled

This is relatively complex, here LZ Use the MySQL , and in MySQL To explain how to solve this garbled problem

As we all know, both the underlying use is purely JDBC or is Hibernate or is JPA Well, it's essentially JDBC , the corresponding framework is only a specific encapsulation on the basis of the relevant. So whatever the technology, it will use the URL that connects to the database . So the URL needs to be checked first.

1.url

The standard situation will be followed by the corresponding character set settings, as follows

Jdbcurl                =jdbc:mysql:///itcastoa?useunicode=true&characterencoding=utf-8


as above, Useunicode it goes without saying that the character sets set in the connection database, &amp What is this? That's a problem, &amp is the escape character of & in XML. If you are using XML to configure the corresponding database connection configuration, then what is the problem. But if you have a problem with **.properties , you must remove the amp . This is really the LZ experienced a badly beaten experience ah.

2. Database


The problem here is also relatively difficult to handle, log in to the database


i: View the database encoding format



can see Server the character set or latin1 , you need to talk about the commonly used character set.


for the peace and prosperity of the world, ISO the organization has specified a set of Unicode Character Set scheme, Unicode coding is a bridge between different encodings that communicate with each other, including + bit binary, so it can accommodate 2 of the to The character of the second party, the lifetime is enough. And according to different needs of Unicode is divided into three kinds of programs.

Utf8 : Codes that appear to address different languages in the world. The English language is 8 -bit, while Chinese is 3 -bit. Can be displayed on any browser that supports the UTF9 character set, no additional processing is required.

the other two types are UTF16 and the + , no longer be authored here. You can check it out by yourself, and it is always convenient to store and use to decide which one to use.

Well, it's also relatively familiar. GBK , commonly known as GB Code, the national standard to be formulated, only contains Chinese characters. As a result,theUTF8 is better compatible, but the amount of storage is greater.

immediately back, roughly the character set charge or back to solve the problem first. So it is possible to use GBK or UTF8 . But latin1 must not be able to, here mainly through such a command to set

Set the encoding for the server, database, and data table sections, respectively, and you must set the connection encoding. The connection encoding settings are as follows:

mysql> SET character_set_client= ' GBK ';

mysql> SET character_set_connection= ' GBK '

mysql> SET character_set_results= ' GBK '

Set the code, the following can be successfully inserted in Chinese, in fact, in a word can be solved

Common commands

View the database encoding format

Show variables like ' character_set_% ';

To view the creation of a table in a database

Show CREATE TABLE tablename;

Set the database encoding format

Setnames ' GBK ';


Well, very late, today's summary of the first here, the Chinese garbled problem is an over, the problem is not very big, but very common, particles. Wash up and sleep.

MySQL Write Chinese garbled

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.