Java operation MySQL Chinese processing method

Source: Internet
Author: User

1, establish a class, including a format to convert Chinese to iso8859-1 encoding method:

publicclass Format2Chinese {
public Format2Chinese() {
}
public String format2IS08859(String str) {
try {
if (str == null    str.trim().equals(""))
str = "";
else
str = new String(str.getBytes("ISO8859-1"));
} catch (Exception e) {
e.printStackTrace();
}
return str;
}
}

2, you should add something in your bean, such as the following simple bean:

publicclass Leavemsg
{
public Leavemsg() {
}
//这个要加的,因为写入的时候我们不格式,写出的时候格式化
public Leavemsg(boolean format) {
this.format = format;
}
private String msg;
booleanformat = false; //用于确定是否将字符格式转换
Format2Chinese function = new Format2Chinese();
publicvoid setMsg(String msg) {
if (format) {
this.msg = function.format2IS08859(msg);
} else
this.msg = msg;
}
public String getMsg() {
return msg;
}
}

3, in the use of beans to load data, this statement:

Leavemsg msg=new leavemsg (true); Then all the other operations are the same, OK.

When the page shows the Chinese time will not be a problem, in the JSP page this can be done with this kind of packaging to get content.

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.