Solutions to garbled data reading in Jsp programs or writing data to DB
In JAVA-based programming, Chinese characters are frequently encountered and displayed, such as a lot of garbled characters or question marks.
This is because the default encoding method in JAVA is UNICODE, and the files and DB commonly used by Chinese people are based on GB2312 or BIG5 encoding, so this problem occurs. In the past, I often worried about this problem. After checking some information, I finally solved the problem. I know that many of my friends will also encounter this problem, so I just summarized it, to share with you.
1. Output Chinese characters on the webpage.
JAVA is used in network transmission encoding is "ISO-8859-1", so in the output need to be converted, such:
String str = "Chinese ";
Str = new String (str. getBytes ("GB2312"), "8859_1 ");
However, if the encoding used during program compilation is "GB2312" and the program runs on the Chinese platform, this problem does not occur. Be sure to pay attention to this problem.
2. Read Chinese from Parameters
This is exactly the opposite of the output in the webpage:
Str = new String (str. getBytes ("8859_1"), "GB2312 ");
3. Questions about how to operate databases in Chinese
A simple method is to set "region" to "English (USA)" in "control plane )". If garbled characters still appear, you can perform the following settings:
When retrieving Chinese characters: str = new String (str. getBytes ("GB2312 "));
Input Chinese characters to DB: str = new String (str. getBytes ("ISO-8859-1 "));
4. Solutions for Chinese in JSP:
In "control plane", set "region" to "English (USA )".
Add:
If not, perform the following conversion:
For example: name = new String (name. getBytes ("ISO-8859-1"), "GBK ");
There will be no Chinese problems.