Oracle字符集引起的乱码,可以通过Filter统一处理吗?或者有没有更好的简便方法? 完美解决的话,加送200分 应用服务器没有作到这一步,它只管bean(后台)与jsp(页面)之间信息的一致性,也就是说:bean是gbk那filter就配置gbk,而不是big5。Oracle的JDBC连接机制没有能够指定字符集的,能不能考虑用存储过程来作? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 是以前存在数据库的数据读出都是乱码?还是连现在存入的数据也都是乱码?1、如果是以前存的数据是乱码,那你就只能把以前的数据重新存了,因为你以前存的时候是用SIMPLIFIED CHINESE_CHINA.ZHS16GBK存入的,而现在要用AMERICA_AMERICA.US7ASCII取出,不可能,这是数据库的问题,应该从数据库着手。2、如果是连现在存入的数据也都是乱码,那你在存入的时候就要进行字符集转换。public static String toChinese(String pstrWord) throws java.io.UnsupportedEncodingException { if ((pstrWord != null) && (!pstrWord.equals(""))) { pstrWord = new String(pstrWord.getBytes("ISO8859_1"), "GBK"); return pstrWord; } return ""; } public static String ex_chinese(String str){ if(str==null){ str ="" ; } else{ try { str = new String(str.getBytes("iso-8859-1"),"gb2312") ; } catch (Exception ex) { } } return str ; }public static String toReChinese(String pstrWord) throws java.io.UnsupportedEncodingException { if ((pstrWord != null) && (!pstrWord.equals(""))) { pstrWord = new String(pstrWord.getBytes("GBK"), "ISO8859_1"); return pstrWord; } return ""; }用这几个方法试一下吧。 客户变态,我以前也见过这个问题的客户,主要是客户oracle原来用的那个字符集,后来说服客户了。 to:liaohaiying(小菜) 你的方法是可以实现。问题正如我所说,jsp页面数量巨大,这才是叫人头疼的。to:zhugang(jumbo) 所有的字符处理都调用同一个方法去处理?我不大明白你的意思。一方面我使用动态生成SQL语句,返回的结果集都用同一方法处理,势必要提前转换查询结果。这个工作量还是恐怖。或者我的理解不对? 应该可以使用filter做统一转换,不知JDBC 3.0 public class SetCharacterEncodingFilter implements Filter {// ---------------------- Instance Variables protected String encoding = null; protected FilterConfig filterConfig = null; protected boolean ignore = true;// ---------------------- Public Methods public void init(FilterConfig filterConfig) throws ServletException { this.filterConfig = filterConfig; this.encoding = filterConfig.getInitParameter("encoding"); String value = filterConfig.getInitParameter("ignore"); if (value == null) this.ignore = true; else if (value.equalsIgnoreCase("true")) this.ignore = true; else if (value.equalsIgnoreCase("yes")) this.ignore = true; else this.ignore = false; } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException{ // Conditionally select and set the character encoding to be used if (ignore || (request.getCharacterEncoding() == null)){ String encoding = selectEncoding(request); if (encoding != null) request.setCharacterEncoding(encoding); } // Pass control on to the next filter chain.doFilter(request, response); } public void destroy() { this.encoding = null; this.filterConfig = null; }// ------------ Protected Methods protected String selectEncoding(ServletRequest request) { return (this.encoding); }} TO singedcat(以梦为马) :你写的过滤器是tomcat自己带的吧?一样没有用处的 呵呵,设计没有考虑层的问题!简单的办法就是临时增加一个数据处理层。找一个Encoding JDBC Driver,或者直接写一个,在写和读数据的时候增加Encode处理即可。 to:TinyJimmy(Jimmy) 以前我也好像看到一个jdbc driver,在连接的时候可以指定编码。只是现在找不到了。麻烦推荐一个如何?To GJA106(中文字符) 不知道兄弟有什么成熟的想法没有,可否鉴戒 ? 跨域名 设置/获取 Session 分请问页面分页怎么写?? JSP入门问题 java email 如何获取某方法的实例对象? 初级问题啊~~ 大虾们给个说法吧~~~ 为什么我启动tomcat是窗口一晃而过,我没有自动开机加载他 关于JavaBean的问题 用JSP提取ORACLE的结果集怎么才可以既能向上也能向下滚动?急,在线等待! 请教一个有点比较常见的问题,初学者都会碰到的东西jdbc? 请大家帮忙看一个ireport 调用子表的问题,好郁闷呀~~~ 有趣-奇怪:问题---希望大家讨论
1、如果是以前存的数据是乱码,那你就只能把以前的数据重新存了,因为你以前存的时候是用SIMPLIFIED CHINESE_CHINA.ZHS16GBK存入的,而现在要用AMERICA_AMERICA.US7ASCII取出,不可能,这是数据库的问题,应该从数据库着手。
2、如果是连现在存入的数据也都是乱码,那你在存入的时候就要进行字符集转换。
public static String toChinese(String pstrWord) throws java.io.UnsupportedEncodingException
{
if ((pstrWord != null) && (!pstrWord.equals("")))
{
pstrWord = new String(pstrWord.getBytes("ISO8859_1"), "GBK");
return pstrWord;
}
return "";
}
public static String ex_chinese(String str){
if(str==null){
str ="" ;
}
else{
try {
str = new String(str.getBytes("iso-8859-1"),"gb2312") ;
}
catch (Exception ex) {
}
}
return str ;
}public static String toReChinese(String pstrWord) throws java.io.UnsupportedEncodingException
{
if ((pstrWord != null) && (!pstrWord.equals("")))
{
pstrWord = new String(pstrWord.getBytes("GBK"), "ISO8859_1");
return pstrWord;
}
return "";
}
用这几个方法试一下吧。
你的方法是可以实现。问题正如我所说,jsp页面数量巨大,这才是叫人头疼的。to:zhugang(jumbo)
所有的字符处理都调用同一个方法去处理?我不大明白你的意思。一方面我使用动态生成SQL语句,返回的结果集都用同一方法处理,势必要提前转换查询结果。这个工作量还是恐怖。或者我的理解不对?
this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
String value = filterConfig.getInitParameter("ignore");
if (value == null)
this.ignore = true;
else if (value.equalsIgnoreCase("true"))
this.ignore = true;
else if (value.equalsIgnoreCase("yes"))
this.ignore = true;
else
this.ignore = false;
} public void doFilter(ServletRequest request,
ServletResponse response,
FilterChain chain) throws IOException, ServletException{
// Conditionally select and set the character encoding to be used
if (ignore || (request.getCharacterEncoding() == null)){
String encoding = selectEncoding(request);
if (encoding != null)
request.setCharacterEncoding(encoding);
}
// Pass control on to the next filter
chain.doFilter(request, response);
} public void destroy() {
this.encoding = null;
this.filterConfig = null;
}// ------------ Protected Methods protected String selectEncoding(ServletRequest request) {
return (this.encoding);
}
}
TO singedcat(以梦为马) :
你写的过滤器是tomcat自己带的吧?一样没有用处的
简单的办法就是临时增加一个数据处理层。找一个Encoding JDBC Driver,或者直接写一个,在写和读数据的时候增加Encode处理即可。