The solution of garbled problem in inserting data into database from JSP page

Source: Internet
Author: User

Problem Description: Recently I was writing a Java EE Message board system module, encountered a very big problem to my head, when I input data from the JSP page, through Hibernate business logic class HQL statement to insert this data into the local MySQL database, But when I found that the successful insert in the database is garbled, and then back to the browse page to see is also a bunch of garbled, my JSP page set code for UTF-8, as follows:

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

Then I check my local database encoding is also UTF-8, as follows:

Table operation row number type collation size superfluous
Tb_manager Browse Structure Search Insert Empty Delete 2 InnoDB Utf8_general_ci KB -
tb_reply Browse Structure Search Insert Empty Delete 5 InnoDB Utf8_general_ci KB -
Tb_topic Browse Structure Search Insert Empty Delete 19 InnoDB Utf8_general_ci KB

But the problem is still not solved, I all kinds of Baidu can not solve, I think, the data is inserted in the database before it has been garbled or inserted into the database after the garbled it?

So I'm going to print the data I want to insert into the console before inserting the database, which is a bunch of garbled characters. Online said the General page encoding is Iso-8859-1 I wrote a way to get the data first converted to UTF-8 in the database

The code for the method is as follows:

public string Tochinese (string strvalue) {
try {
if (strvalue==null| | Strvalue.equals ("")) {
Return "";
} else {
strvalue = new String (strvalue.getbytes ("Iso8859_1"), "UTF-8");
return strvalue;
}
} catch (Exception e) {
Return "";
}

}

After writing the data after the conversion of the data to print to the console, found garbled has been successfully converted to Chinese, but the problem came, the number of databases or garbled, then I understand (data inserted before inserting will pass the code conversion), good pit Father Ah (*^__^*), The data entered on the JSP page converts the data into iso8859-1, and the process of inserting it into the database will also convert the data to another encoding, and the number of times it will pass through the encoding. O (∩_∩) o~ I wrote this SQL statement in the database

Show variables like ' char% ';

variable_name Value
Character_set_client Utf8mb4
Character_set_connection Utf8mb4
Character_set_database Latin1
Character_set_filesystem Binary
Character_set_results Utf8mb4
Character_set_server Latin1
Character_set_system Utf8
Character_sets_dir C:\xampp\mysql\share\charsets\

A look to know that the database is not UTF-8 ah, I go to O (∩_∩) o~, this show my database settings encoding is Latin1, I really drunk Latin1 is iso-88859-1 alias Ah, front white turn,. I added this in my Hibernate config file and set the database connection to UTF-8 to solve the problem.

<!--database connection URL--
<property name= "Connection.url" >jdbc:mysql://localhost:3306/db_board? Useunicode=true&amp;characterencoding=utf-8 </property>
The diagonal section above

Again use show variables like ' char% '; query my database encoding, the result is as follows:

variable_name Value
Character_set_client Utf8mb4
Character_set_connection Utf8mb4
Character_set_database Utf8
Character_set_filesystem Binary
Character_set_results Utf8mb4
Character_set_server Latin1
Character_set_system Utf8
Character_sets_dir C:\xampp\mysql\share\charsets\

Finally re-test, inserted into the database of the number of data finally perfect display in Chinese, is really ~~o (>_<) o ~ ~ Tears, this problem engaged me for two days, the morning sleep not 5 points more wake up, also admire my own, finally Meimei sleep a sleep, O (∩_∩) o haha ~

Summary: In fact, looking back, I was wrong in the beginning to think that their database is UTF-8, the collation of the wrong thought is UTF-8, in fact, with show variables like ' char% '; to know what your database encoding is, In fact, I wrote the above method is to get the JSP page to the data encoding from UTF-8 to iso-8859-1, and then inserted into the database, will be garbled only the database is not set to UTF-8, the Hibernate configuration file after the database connection add ? Useunicode=true&amp;characterencoding=utf-8 This sentence is good, so that the database encoding is set to UTF-8

The solution of garbled problem in inserting data into database from JSP page

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.