Mysql garbled problem analysis and solution _ MySQL

Source: Internet
Author: User
Mysql garbled problem analysis and solution: During the development of bitsCN.com, we can't avoid any disgusting garbled code or a series of problems caused by garbled code. Garbled characters are the cause of character sets. they generally do not have much to do with the logic. that is to say, there is no problem in the overall direction of the system. a vulnerability occurs in a small place, which causes the program to fail to run normally, so garbled text is a very disgusting thing. Here, we will briefly introduce the garbled problem we have encountered, the ideas in the process of solving the problem, and the general operations. what we need to learn is how to analyze the problem and solve the problem, the difference between "fish" and "fish" is not just to solve the current difficulties again and again based on the operations on the Internet.

Background:
The function to be implemented is very simple. use dom4J to read the XML file and then use Spring and Hibernate to write data to the MySql database (multiple tables). of course, the whole process is completed by Spring to control the transaction at one time, rollback is performed due to an exception.
Symptom: an error is reported when half of data is written to the database. the general error message is that the record is not unique.
Analysis and Operations:
Check your code and use HibernateTemplete in Spring to save the data.

Observe again: it seems to be a problem with foreign key constraints, because every time data is inserted into a table with foreign keys, it cannot be input.
Analysis and Operations:
Since it is a foreign key table problem, we should first determine what the content in the foreign key table is, so manually change the database isolation level to view the data, which is indeed garbled!

Q: How do I access the database with garbled characters?
Analysis and Operations:
It is garbled when reading xml, or when writing data to the database. Breakpoint debugging again to test whether the code is garbled in the program.

Symptom: no garbled information is printed in eclipse.
Analysis and Operations:
This shows that there is no problem in the program and it is a database problem. So first change the database link string to specify the encoding, then change the database encoding, and then breakpoint debugging again.

Symptom: The database is still garbled, but the garbled form is changed. it was all "?". It's a mess of text.
Analysis and Operations:
Obviously, the above series of operations to modify the encoding format work, but they are not modified. Check the encoding formats again. no problems are found.
The problem was once deadlocked ......
For help: it is too difficult to use the command window. use a third-party client, so use a third-party client. The strange thing is that the client shows no problem!
Analysis and Operations:
If the client has no problem with the command window, it indicates that the database is not garbled, and garbled characters may be displayed on the command line. Set the command line display characters, and the display is normal! Test data import again ---- everything is OK.

Thoughts and summary:
Symptom: in fact, this problem was solved when the encoding was modified for the first time, but because the program was not executed during the breakpoint debugging, it was always considered that the database was still garbled.
Summary:
The program should be completed during each debugging and the error context should be simulated, rather than focusing on the error itself.

Symptom: XML encoding, dom4j reading encoding, database encoding, database connection string encoding, and command line window display encoding are involved here. Which of the above cannot solve the problem (I am not aware of the last one ).
Summary:
The concept of concept is to grasp every control variable, from the beginning to the end, consciously jump out of the environment to make assumptions.
BitsCN.com

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.