[Mysql] Mysql database processes Chinese characters transmitted in Servlet, mysqlservlet
The default encoding of many Mysql instances is latin1. If Mysql does not change the encoding during installation, it cannot be entered into Chinese directly and garbled,
First, change the encoding of the field to be stored in Chinese from latin1 to UTF-8. If MySQLQueryBrowser is used, you can open the table to be modified, click the field to be modified, and modify it to UTF-8 in Column Charset of Column details.
In the Servlet Java file, must the database be connected with parameters? UseUnicode = true & characterEncoding = utf8 & useOldAliasMetadataBehavior = true
At the same time, Servlet may use form forms to pass Chinese characters between jsp pages and java files, which may also contain garbled characters.
In case of garbled characters, use <% @ page import = "java.net. *" %>
Then, in the form of passing Chinese characters, the string is transcoded to UTF-8 through URLEncoder. encode ("string", "UTF-8 ").
Then, in the Java file that needs to be received using request. getParameter, use URLDecoder. decode (request. getParameter ("parameter name defined by string"), "UTF-8"); decode
In this way, no garbled characters will be transmitted into the database.
For databases during the debugging process, you can use Truncate Table to clear the entire Table. The most important thing is that the auto-increment field value can be set to zero.
Truncate is an SQL syntax that can quickly clear all data in a data table. In addition, it can reset the count to zero and re-calculate the fields with auto-increment values.
Chinese character garbled characters in jsp + servlet + mysql
The first three steps can be solved. If not, go to step 4.
① First, stop MySQL services in the run window. Input: net stop mysql ② change the server and client character set to the desired character set: gbk ...... The specific operation is: open my. ini in the mysql installation directory, find default-character-set, and change it to the desired character set: gbk ......, Note that there are two default-character-set, which will be found by pressing ctrl + f at the beginning of the file and entering default. You have to change the settings. ③ restart the MySQL server, in the running window, enter net start mysql 4. The most important thing is that we have been able to solve the garbled problem here, but the problem is that we still have garbled issues, this is because the default character set (latin1) is used when the current table is created. In this case, we need to delete the table and then recreate it.
Servlet processes form submission data and connects to mysql database to match
Ps = conn. prepareStatement ("select * from USER where ID =? And PAW =? "); Ps. setObject (1, user); ps. setObject (2, passwr );
Set Parameters Based on the Data Type of your database. Do not use objects.