如何解决从数据库中提取的汉字出现乱码的情况? 获取数据源的那段java代码url使用如下格式jdbc:mysql://localhost:3306/dbname?useUnicode=true&characterEncoding=GBK 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 1.首先:保证在数据库中保存的文字是中文的。(查看数据库中保存的记录)2.其次:保证所输出的页面已经使其为能够显示中文的(设置其contentType)。3.如果以上条件均符合,但仍为乱码,那么可以考虑编码转换。 建议用编码转换,如下://***************************************************//名称:ChangeCode//功能:转换指定字符串的字符集(字符编码)//输入:strSource: 要转换的字符串; strCodeFrom: 源字符集; strCodeTo: 目的字符集//输出://返回:转换之后的字符串//***************************************************public String ChangeCode(String strSource, String strCodeFrom, String strCodeTo){ byte[] baTemp = null; try { baTemp = strSource.getBytes (strCodeFrom); strSource = new String (baTemp,strCodeTo); } catch (Exception e) { return (e.toString ()); } return (strSource);}///aStrSql = ChangeCode (aStrSql, "8859_1", "cp850"); 字符集所使用的编码格式不同,必须要进行编码转换String newStr = s.getBytes("8859_1") 1.s=new String(s.getBytes(),"iso-8859-1");2.s=new String(s.getBytes(),"gb2312");3.s=new String(s.getBytes("iso-8859-1"),"gb2312");4.s=new String(s.getBytes("gb2312"),"iso-8859-1");5.s=new String(s.getBytes("iso-8859-1"));6.s=new String(s.getBytes("gb2312"));尝试以上的方法之一! JVM调优的问题 java方面有什么赛事啊? 谁能告诉我怎么把以前的老项目转变成WTP项目 请教一个IP段过滤的问题 麻烦高手解决小程序的稳定性*/40分/* 高手近来 问下ISO 10646和unicode的区别 在线等 请教:MIDlet是什么东西啊? 大家好! 求regionMatches的使用实例! 大哥们,小弟我准备学JAVA了给推荐几本好书吧!小弟不胜感激啊! 有关DecimalFormat格式化字符串的问题??? 内部类的问题
2.其次:保证所输出的页面已经使其为能够显示中文的(设置其contentType)。
3.如果以上条件均符合,但仍为乱码,那么可以考虑编码转换。
//***************************************************
//名称:ChangeCode
//功能:转换指定字符串的字符集(字符编码)
//输入:strSource: 要转换的字符串; strCodeFrom: 源字符集; strCodeTo: 目的字符集
//输出:
//返回:转换之后的字符串
//***************************************************
public String ChangeCode(String strSource, String strCodeFrom, String strCodeTo)
{
byte[] baTemp = null;
try
{
baTemp = strSource.getBytes (strCodeFrom);
strSource = new String (baTemp,strCodeTo);
}
catch (Exception e)
{
return (e.toString ());
} return (strSource);
}///
aStrSql = ChangeCode (aStrSql, "8859_1", "cp850");
2.s=new String(s.getBytes(),"gb2312");
3.s=new String(s.getBytes("iso-8859-1"),"gb2312");
4.s=new String(s.getBytes("gb2312"),"iso-8859-1");
5.s=new String(s.getBytes("iso-8859-1"));
6.s=new String(s.getBytes("gb2312"));
尝试以上的方法之一!