When the JSP page is used to add Chinese data to the MySQL database, it is found to be garbled or garbled when reading Chinese data from mysql. The root cause of these problems is the inconsistency of character encoding. We need to know the source of the disease before we can take the right medicine, so I will briefly describe the problem generation process (if any, please kindly advise me)
Here is an example of reading data from the form and writing data to the MySQL database;
HTML page parameters-(sent in browser encoding mode)-"JSP page-(compiled in JSP-defined encoding mode)-" are compiled by the JSP Engine *. class file -- run in JSP Container -- pass to MySQL database-(read and store data by database encoding)-read mysql-(read by database encoding) -The JSP page is displayed in the browser after it is accepted (decoded by the browser );
Note: The "browser encoding", "JSP page encoding", "JSP page output encoding", "JSP page input encoding", and "database encoding" may not always exist, if any of them is different, garbled characters may appear. Just like a messaging game, James needs to pass the words to Lee IV, and Lee needs to send the words he heard to Wang Wu, to let Wang Wu know what James said, he must use the same language as the three of them. If Jack is a Martian, he certainly cannot understand what James said, then he said nothing to Wang Wu about Mars (garbled characters). As a result, Wang Wu could only hear garbled characters. (Compare to sweat ~~ ^_^ !)
Now we can change the encoding methods to the same one;
// ------------------------- Browser code -----------------
<HTML>
<Head> <meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"> </Html>
// --------------------------- JSP page -------------------
<% @ Page pageencoding = "gb2312" %> // JSP page code:
<% @ Page contenttype = "text/html"; charset = gb2312 "%> // JSP page output code:
<% Request. setcharacterencoding ("gb2312"); %> // enter the encoding on the JSP page:
// ------------------------ MySQL code -------------------
Connection connect = java. SQL. drivermanager. getconnection (
"JDBC: mysql: // localhost/mydb? User = login account & Password = your password & useunicode = true & characterencoding = gb2312 ");
//-------------------------------------------------------
Note: The gb2312 writing method (the red part) should be differentiated in the browser and database!
The configuration is successfully tested! (Tomcat5.5 + mysql5.0)
// I have read a lot of data online. Article For example, function transcoding ~~ Modify the configuration file ~~ It is not as simple as this and the success rate is high!
Recommended!