数据库存储最好用8859_1的格式,
所以存入数据库的时候进行一下编码转换,但我们通常的显示格式为GB2312或GBK,所以取出来的时候再转一次
例如:
存入数据库时用:
把数据转成8859_1的格式
name=new String(name.getBytes("gb2312"),"8859_1");
content=new String(content.getBytes("gb2312"),"8859_1");
insert into (name,content)values(?,?).......
从数据库取的时候用:
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/gfqqqqpe?user=gfqqqqpe_f&password=aaaaa&useUnicode=true;characterEncoding=8859_1");
从数据库取的时候指定编码方式为“8859_1”,
然后显示的时候进行转换:
name=new String(name.getBytes("8859_1"),"gb2312");
这种方法在MYSQL上通用
//处理中文的自定义函数
<%!
public String getStr(String str)
{
try
{
String temp_p=str;
byte[] temp_t=temp_p.getBytes("ISO8859-1");
String temp=new String(temp_t);
return temp;
}
catch(Exception e)
{
e.printStackTrace();
}
return "null";
}
%>
博客介绍了MySQL数据库存储中文时的编码转换方法。存入数据库时,将数据转成8859_1格式;从数据库取出时,指定编码方式为8859_1,显示时再转换为GB2312或GBK。还给出了处理中文的自定义函数示例。

被折叠的 条评论
为什么被折叠?



