Hibernate + mysql garbled

Source: Internet
Author: User

Today, I wrote a simple sample program using spring + hibernate + struts2, using spring to manage hibernate for data processing, integrating struts2, and using webwork for a long time, the use of struts2 is also quite easy. It shows that the test data went smoothly. When the data entry function was started, a problem occurred while Inputting Chinese characters, and all the entered Chinese characters became garbled.

At first, I thought that there was a problem with Chinese character transmission in tomcat. I added a Chinese filter, but the problem was not solved. Suddenly I thought that the attribute file in struts2 had set the encoding to gbk: struts. properties
Struts. i18n. encoding = GBK
Struts. objectFactory = spring should not be a problem during the transmission process. input the data of the input Action and Dao in the background, and all the data can be displayed normally. Then I suspected that it was a problem with mysql settings. I found the method for setting the mysql database encoding. The problem was still not solved after the settings. Directly execute Insert into user ('Nam ') values ('Chinese character') in mysql. After mysql executes the Insert into user ('Nam ') command, the data is displayed normally, indicating that the database encoding is correct. Depressed. What is the problem? No Chinese garbled characters were found before the save () method of hibernateTemplate was executed. In the database, no Chinese garbled characters were also tested. Only the problem at the hibernate Persistence Layer was found. Looking for garbled characters when the persistence layer transmits data to the database server, we finally found the problem: The hibernate Persistence Layer does not set the encoding format during transmission, in this way, the data is garbled after it is transmitted to the database server, and there is no transmission garbled problem in oracle and sqlserver, so I did not think of it as a problem here. Modify the configuration parameters of the data source in spring: database connection method:

Jdbc: mysql: // localhost: 3306/springexample? CharacterEncoding = UTF-8
After the data is entered, the Chinese character is displayed normally. When using hibernate to manage mysql databases, you should not only pay attention to the encoding of web servers and databases, but also the encoding of data transmission at the persistence layer.

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.