Home  >  Article  >  Java  >  What should I do if java inserts data into mysql database and displays garbled characters?

What should I do if java inserts data into mysql database and displays garbled characters?

coldplay.xixi
coldplay.xixiOriginal
2020-06-13 09:18:472944browse

What should I do if java inserts data into mysql database and displays garbled characters?

What should I do if java inserts data into mysql database and displays garbled characters?

Solution to the garbled code displayed when inserting data into mysql database in java:

1. Make sure whether the character set filter is added:

In web.xml in the java web project, add the following code to solve the garbled code:

<filter>
        <filter-name>SpringEncodingFilter</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>
    <filter-mapping>
        <filter-name>SpringEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

2. Make sure the created database is configured to support Chinese, configure as shown in the figure below:

What should I do if java inserts data into mysql database and displays garbled characters?

3. If the code is still garbled, it is a mysql encoding problem (the default encoding of mysql is: latin1):

1. Check the mysql encoding configuration:

"Run" -> Enter "mysql" -> Pop up the mysql client dialog box -> Enter mysql as root:

After entering: Enter the command: "show variables like 'character% ';" Check the mysql character encoding, the results are as follows:

What should I do if java inserts data into mysql database and displays garbled characters?

It means that Chinese encoding is not supported, the operation is:

In the installation directory of mysql , find the configuration file "my.ini" (the suffix is ​​ini under the Windows system, and the suffix is ​​cnf under the Mac system), open it in Notepad mode:

Find the client configuration [client], add it below "default-character-set=utf8", then find [mysql], add "default-character-set=utf8" below, then search for "default-character-set", and change all default-character-set to "utf8" (Note: Usually after changing [client] and [mysql], the remaining positions also need to be changed), as shown below:

What should I do if java inserts data into mysql database and displays garbled characters?

Then restart the mysql service and "show variables like 'character%';" again to view the mmysql character encoding. The results are as follows:

What should I do if java inserts data into mysql database and displays garbled characters?

Then test adding data to the database , the garbled code problem no longer occurs.

Recommended tutorial: "JAVA Video Tutorial"

The above is the detailed content of What should I do if java inserts data into mysql database and displays garbled characters?. 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