在从oracle数据库中读取数据时出现乱码,但是通过pl/sql developer查询正常。具体情况如下:
jsp,页面编码:gb2312
oracle编码:us7ascii
使用过滤器进行编码转换,request.setCharacterEncoding(gb2312),能够使传参时编码转换成中文,但是从数据库中读来的中文却显示为乱码。
如果不使用过滤器,且去掉jsp页面中page指令的charset=gb2312这句话,中文却能正常显示,很是疑惑。
现在我使用的办法每次把从数据库中取来的数据进行String(s.getBytes("ISO8859-1"), "gb2312")转换,但是这样做很麻烦,想着能不能编写个过滤器之类,把每次从数据库取来的数据进行编码转换?

解决方案 »

  1.   

    还是用utf-8吧,gb2312很多字显示不了,乱码,字符集包含的汉字太少了...
      

  2.   

    你可以写个jdbc的类,把数据库的数据读出来后,文本、字符串类型的,直接转换编码存在结果里:private int executeQuery(final String sql, final List params) throws AppException {
        int iResult = -1;
        setListResult(new ArrayList<Map>());
        conn = getConnection();    try {
            try {
                pstmt = conn.prepareStatement(sql);
            } catch (SQLException se) {
                throw new AppException(this.getClass().getName() + ":_executeQuery(final String sql, final List params):conn.prepareStatement(" + sql + ")失败。SQLException错误信息:" + se.getMessage());
            }
            Iterator inputParamsIte = params.iterator();
            int index = 1;
            try {
                while (inputParamsIte.hasNext()) {
                    pstmt.setObject(index, inputParamsIte.next());
                    index++;
                }
            } catch (SQLException se) {
                throw new AppException(this.getClass().getName() + ":_executeQuery(final String sql, final List params):pstmt.setObject(index, inputParamsIte.next())失败。object:" + inputParamsIte.toString() + "。SQLException错误信息:" + se.getMessage());
            }        try {
                rs = pstmt.executeQuery();
                ResultSetMetaData rsmd = rs.getMetaData();
                while (rs.next()) {
                    Map map = new HashMap();
                    for (int i = 1; i <= rsmd.getColumnCount(); i++) {
                        if (rsmd.getColumnTypeName(i).equals("text")) {
                            StringBuffer sbf = new StringBuffer();
                            java.io.InputStream ins = rs.getAsciiStream(i);
                            int ch = 0;
                            if (ins != null) {
                                try {
                                    while ((ch = ins.read()) != -1) {
                                        sbf.append((char) ch);
                                    }
                                    ins.close();
                                    map.put(rsmd.getColumnName(i), new String(sbf.toString().getBytes("8859_1"), "GB2312"));
                                } catch (IOException ioe) {
                                    throw new AppException(this.getClass().getName() + ":_eexecuteQuery(final String sql, final List params):pstmt.executeQuery()失败。IOException错误信息:" + ioe.getMessage() + "。sql:" + sql);
                                }
                            } else {
                                map.put(rsmd.getColumnName(i), null);
                            }
                        } else {
                            map.put(rsmd.getColumnName(i), rs.getObject(i));
                        }
                    }
                    getListResult().add(map);
                }
            } catch (SQLException se) {
                throw new AppException(this.getClass().getName() + ":_executeQuery(final String sql, final List params):pstmt.executeQuery()失败。SQLException错误信息:" + se.getMessage() + "。sql:" + sql);
            }
            iResult = getListResult().size();
            logger.debug("executeQuery:" + sql + ",successe.");
        } finally {
            closeSqlObject();
        }    return iResult;
    }
      

  3.   

    重点是
    if (rsmd.getColumnTypeName(i).equals("text")) {
    ... ...
    }else{
    ... ...
    }
      

  4.   

    你要寫成過濾器,不要用硬編碼:使用过滤器进行编码转换,request.setCharacterEncoding(gb2312)
      

  5.   

    感觉还是用GBK好 些啊 。
      

  6.   

    是想写成过滤器,但是过滤器只对request过滤,没用啊
      

  7.   

    utf-8 好用 貌似struts2里面进行了对utf-8的封装