有什么方法解决 java 程序到 数据库中文乱码的问题.
我是用jbuilder+mysql.
用什么方法?
 知道的多罗列几种.详细点好.
谢谢

解决方案 »

  1.   

    其实mysql解决了中文问题就可以了。你可以在配置mysql的时候选择gb2312.网上有处理的具体方法,去看看吧!很管用的。
      

  2.   

    在设置URL的时候就可以直接搞定了吧URL="jdbc:mysql://localhost/dbname?user=username&password=password&useUnicode=false&characterEncoding=gbk"这样应该就可以了
    当然也可以对传入DB的字符做编码规定,String ss;
    String s = new (ss.getBytes("iso-8859-1"),"gbk");
      

  3.   

    写个Filter在web.xml中配置下
    public class EncodingFilter extends HttpServlet implements Filter { private static final long serialVersionUID = 1L; /* 过滤器引用 */
    private FilterConfig filterConfig = null; /* 字符集变量 */
    private String charSet = null; /* 过滤器转换字符集 */
    public void doFilter(ServletRequest request, ServletResponse response,
    FilterChain chain) throws IOException, ServletException {
    try {
    /* 获得应用程序配置的字符集 */
    charSet = (String) filterConfig.getInitParameter("charSet"); /* 设置Http字符集的请求为系统的默认字符集 */
    HttpServletRequest req = (HttpServletRequest) request;
    req.setCharacterEncoding(charSet); /* 获得Http的响应 */
    HttpServletResponse res = (HttpServletResponse) response; /* 转发道请求的资源 */
    chain.doFilter(req, res);
    } catch (Exception e) {
    System.out.println("EncodingFilter " + e.getMessage());


    }
    } /* 过滤器销毁 */
    public void destroy() {
    this.filterConfig = null;
    this.charSet = null;
    } /* 过滤器初始化 */
    public void init(FilterConfig filterConfig) {
    this.filterConfig = filterConfig;
    }
    }在web.xml中加入
    <filter>
    <filter-name>EncodingFilter</filter-name>
    <filter-class>com.test.EncodingFilter</filter-class>
    <init-param>
    <param-name>charSet</param-name>
    <param-value>UTF-8</param-value>
    </init-param>
    </filter> <filter-mapping>
    <filter-name>EncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
    </filter-mapping>
      

  4.   

    我上次连数据库也是有问题,有个最简单的方法:
    把所有的字符集都设置为“gb2312”数据库里的也一样选择,
    注意在从web页提交给javaBEAN之前把字符集转换一次为“gb2312”就可以成功提交了。% request.setCharacterEncoding("gb2312");<---先转换,再提交
     %>
    <jsp:useBean id="Customer" scope="page" class="com.ebook.common.Customer"/>
    <jsp:setProperty name="Customer" property="*"/>