Mysql writes Chinese garbled characters, mysql writes Chinese Characters

Source: Internet
Author: User

Mysql writes Chinese garbled characters, mysql writes Chinese Characters

Garbled text is a common problem of programmers. The following describes several common cases. It is common in three aspects: Page, foreground-to-background value transfer, and database writing.

 

1. Page garbled characters

 

Pages are relatively easy to solve. You can set relevant character sets on the corresponding jsp pages or html pages. For example

 

<%@page language="java" contentType="text/html; charset=utf-8"    pageEncoding="utf-8"%>


 

2. garbled values

 

Garbled characters occur frequently during value transfer. First of all, I don't know what the scenario is. There are usually the following common solutions:

Configure the specified filter


 

<! -- Configure the request filter and set the encoding format to UTF-8 to avoid Chinese garbled characters --> <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>



 

Set the request Character Set

 

Garbled characters are often introduced from the front-end to the corresponding controller or action. The general idea is to print the default Character Set of the request.

 

i:System.out.println(request.getCharacterEncoding());


 

Then, set the corresponding character set if the printed character set is not required.

 

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


Of course, in some cases, it may still not be possible to solve this problem.


String str=newString((request.getParameter("bigQuestionTypeName")).getBytes("iso-8859-1"),"utf-8")


 

3. garbled characters stored in the database

 

 

This is relatively complicated. Here, lz uses mysql and uses mysql to introduce how to solve this garbled problem.

 

 

As we all know, no matter whether the underlying layer uses pure jdbc, hibernate, or jpa, it is actually jdbc in essence. The corresponding framework is only encapsulated on the basis of the relevant. Therefore, no matter what the technology is, the url to connect to the database will be used. Therefore, the url must be checked first.

 

1. url

 

The corresponding character set settings will be added to the standard, as shown below:

 

jdbcUrl                =jdbc:mysql:///itcastoa?useUnicode=true&characterEncoding=UTF-8


 

As mentioned above, useUnicode does not need to be mentioned. It connects to the character set in the database. What is this & amp? This is a problem. In xml, & amp is & escape character. If you use xml to configure the corresponding database connection configuration, then what is the problem. However, if you use **. properties, you must remove the amp. This is indeed the experience that lz experienced.

 

2. Database


 

The problem here is also relatively difficult to handle.


 

I: view the database encoding format



 

We can see that the server character set is still latin1. here we need to talk about the commonly used character sets.


For the sake of peace and prosperity in the world, ISO has designated a unicode Character Set scheme. Unicode encoding serves as a bridge between different encodings for mutual conversion, including 32-bit binary, so it can hold 2 to the power of 31 characters, and it will be enough in the lifetime. Unicode is divided into three solutions based on different requirements.

Utf8: Used to address international encoding in different languages. 8 digits are used for English, and 3 digits are used for Chinese. It can be displayed in any browser that supports the utf9 character set without further processing.

The other two types are utf16 and 32, which will not be compiled here. You can check this information on your own. In general, you can decide which one to use because of the convenience of storage and usage.

Well, we are also familiar with gbk, also known as the Country Code. It is formulated by Chinese national standards and only contains Chinese characters. Therefore, utf8 is more compatible than utf8, but has a larger storage capacity.

 

I will be back soon. If the character set is fully charged, I will solve the problem first. Here, both gbk and utf8 are supported. But latin1 is definitely not feasible. Here we mainly use this command to set

 

The server, database, and data table encoding must be set. 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'

 

After the encoding is set, the Chinese characters can be successfully inserted below, which can be solved in one sentence.

 

 

Common commands

 

View database encoding formats

Show variables like 'character _ set _ % ';

View the creation of tables in the database

Show create table tablename;

Set the database encoding format

Setnames 'gbk ';

 


Well, it's very late. Today's summary is here. The Chinese Garbled text problem has come to an end. The problem is not very big, but it is very common. Wash and sleep.

 

 

 

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.