谁有将mysql数据库编码转换的过滤器,我的JSP页面是GBK的,但mysql数据库是ISO8859_1的,现在想用过滤器转换,谁有代码,及实现过程,感激不尽了

解决方案 »

  1.   

    public class SetCharacterEncodingFilter implements Filter { protected String encoding = null; protected FilterConfig filterConfig = null; public SetCharacterEncodingFilter() {
    super(); } /*
     * (非 Javadoc)
     * 
     * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
     */
    public void init(FilterConfig filterConfig) throws ServletException { this.filterConfig = filterConfig;
    this.encoding = filterConfig.getInitParameter("encoding"); } /*
     * (非 Javadoc)
     * 
     * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest,
     *      javax.servlet.ServletResponse, javax.servlet.FilterChain)
     */
    public void doFilter(ServletRequest request, ServletResponse response,
    FilterChain chain) throws IOException, ServletException {
    // Select and set (if needed) the character encoding to be used
    String encoding = selectEncoding(request); if (encoding != null)
    request.setCharacterEncoding(encoding); // Pass control on to the next filter
    chain.doFilter(request, response);
    } /*
     * (非 Javadoc)
     * 
     * @see javax.servlet.Filter#destroy()
     */
    public void destroy() {
    this.encoding = null;
    this.filterConfig = null; } protected String selectEncoding(ServletRequest request) { return (this.encoding); }}
    希望对你有所帮助
      

  2.   

    //数据库连接
      private Connection getConnection() {
        Connection connection = null;    try {
          Class.forName("com.mysql.jdbc.Driver").newInstance();
          String dburl =
              "jdbc:mysql://"
              + "localhost"
              + "/"
              + "mydb"
              + "?user="
              + "root"
              + "&password="
              + ""
              + "&useUnicode=true&characterEncoding=GBK";
          connection = DriverManager.getConnection(dburl);
        }
        catch (InstantiationException e) {
          e.printStackTrace();
        }
        catch (IllegalAccessException e) {
          e.printStackTrace();
        }
        catch (ClassNotFoundException e) {
          e.printStackTrace();
        }
        catch (SQLException e) {
          e.printStackTrace();
        }
        return connection;
      }  public static String toISO8859(String str) {
        if (str == null) {
          return null;
        }
        try {
          return new String(str.getBytes("iso-8859-1"));
        }
        catch (UnsupportedEncodingException e) {
          e.printStackTrace();
        }
        return null;
      }  public static String toGBK(String str) {
        if (str == null) {
          return null;
        }
        try {
          return new String(str.getBytes("ISO8859_1"), "GBK");
        }
        catch (UnsupportedEncodingException e) {
          e.printStackTrace();
        }
        return null;
      }
      

  3.   

    String dburl =
              "jdbc:mysql://"
              + "localhost"
              + "/"
              + "mydb"
              + "?user="
              + "root"
              + "&password="
              + ""
              + "&useUnicode=true&characterEncoding=GBK";
    ---------------------------------------------------------
    强烈不推荐,效率损失太大了!
    写一行不就完了嘛!
      

  4.   


    回LZ:      你把MYSQL的表转化成中文的不好吗? 用工具看数据还是中文的.