Java 中文问题一直困扰许多学习者。总结了下面的一些情况的解决方法。
希望对大家有帮助。
连接 Mysql Database Server:
-------------------------------------------------------------------------------
 mysql 不支持 unicode,所以比较麻烦。
 将 connectionString 设置成 encoding 为 gb2312
 String connectionString
 = "jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=gb2312";测试代码:
       String str = "汉字";
       PreparedStatement pStmt = conn.prepareStatement("INSERT INTO test VALUES (?)");
       pStmt.setString(1,str);
       pStmt.executeUpdate();数据库表格:
 create table test (
   name char(10)
 )
连接 Oracle Database Server
-------------------------------------------------------------------------------
 在把汉字字符串插入数据库前做如下转换操作:
 String(str.getBytes("ISO8859_1"),"gb2312")测试代码:
       String str = "汉字";
       PreparedStatement pStmt = conn.prepareStatement("INSERT INTO test VALUES (?)");
       pStmt.setString(1,new String(str.getBytes("ISO8859_1"),"gb2312");
       pStmt.executeUpdate();
Servlet
-------------------------------------------------------------------------------
 在 Servlet 开头加上两句话:
 response.setContentType("text/html;charset=UTF-8");
 request.setCharacterEncoding("UTF-8");JSP
-------------------------------------------------------------------------------
 在 JSP 开头加上:
 <%@ page contentType="text/html; charset=gb2312" %> 

解决方案 »

  1.   

    汉字的Unicode码
    中国:\u4e2d\u56fd (十六进制) &#20013;&#22269;(十进制)
      

  2.   

    我用java+mysql,没有问题啊。
    只要在 JSP 开头加上:
     <%@ page contentType="text/html; charset=gb2312" %> 
    就显示正常了
      

  3.   

    不用做那么多重复而意义不大的事情我把我的解决方案给你吧:
    1.把连接mysql的url定义为(主机名和数据库名改一下):
      jdbc:mysql://192.168.1.2:3306/worklog?useUnicode=true&amp;characterEncoding=GBK2.在工程里添加一个过滤器:package com.multim.technic.util;import javax.servlet.*;
    import javax.servlet.http.*;
    import java.io.*;
    import java.util.*;/**
     * <p>Title:  工作日志管理系统</p>
     * <p>Description: </p>
     * <p>Copyright: Copyright (c) 2004</p>
     * <p>Company:  </p>
     * @author wingtrace
     * @version 1.0
     */public class SetCharacterEncodingFilter
        extends HttpServlet
        implements Filter {
      protected String encoding = null;
      protected FilterConfig filterConfig = null;
      protected boolean ignore = true;  public void destroy() {
        this.encoding = null;
        this.filterConfig = null;
      }  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 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;
        }  }  protected String selectEncoding(ServletRequest request) {
        return (this.encoding);
      }}3.在web.xml文件里面加入:  <filter>
        <filter-name>setcharacterencodingfilter</filter-name>
        <filter-class>com.multim.technic.util.SetCharacterEncodingFilter</filter-
           class>
        <init-param>
        <param-name>encoding</param-name>
        <param-value>GBK</param-value>
       </init-param>
      </filter>以上解决方案在 tomcat5.0.25+mysql4 中通过。
      

  4.   

    实际项目开发的时候不可能也不允许把字符编码问题分散到各个具体的页面(或servlet/action)去处理,当页面数膨胀到一定程度的时候,这些零散的问题将变的无法控制。一般来说都必须集中处理,例如使用统一的过滤器(一种特殊的servlet),另外还有一种方法是对数据库连接池进行再封装,利用类反射机制重写Connection和Statement等。当然这样做比较难,俺正在研究。:)