java项目 java web项目数据库乱码问题的解决办法

来源:互联网 发布:30元备案域名 编辑:程序博客网 时间:2024/06/11 18:14

作为java菜鸟,在学习到数据库操作的时候往往会遇到一个很头痛的问题,数据库中文乱码。这似乎是每个java程序员在成长过程中都会遇到的问题,下面我就把我自己的几种方法和大家分享一下。(一般是在MyEclipse下开发)

java项目:

首先,确保项目的编码是gbk或utf-8(),在Windows->Preferences->General->Workplace里可以设置。

如果项目是通过xml文件配置数据库(比如hibernate之类的框架,方便数据库的切换),在数据库连接语句如:jdbc:mysql://localhost/test1后加上?characterEncoding=gb2312变成

jdbc:mysql://localhost/test1?characterEncoding=gb2312。

最后把数据库中的字符集修改为gbk2312,字符效验gbk_general_ci或utf-8,utf_general_ci.

好了测试一下吧,如果有可视化操作界面如navicat就能看到中文数据了,如果是dos下查看的话,中文还是“??”,不要着急再查数据之前运行 set names gbk;再查就OK了。

 

java web项目:

完成如上java项目的设置。

确保jsp页的编码为utf-8。

加入全站中文过滤器,过滤器servlet代码如下

 

---------EncodingFilter .java-------

package info.filter;

import java.io.IOException;
import java.util.StringTokenizer;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;

public class EncodingFilter implements Filter {

 protected String encoding = null;

 protected FilterConfig config;

 public void init(FilterConfig filterConfig) throws ServletException {
  this.config = filterConfig;
    this.encoding = filterConfig.getInitParameter("Encoding");
 }
 
 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,ServletException {
  if (request.getCharacterEncoding() == null) {
   
   String encode = getEncoding();
   if (encode != null)
   
    request.setCharacterEncoding(encode);
    response.setCharacterEncoding(encode);
  }
  String ip = request.getRemoteAddr();
  
        HttpServletResponse httpResp = null;

        if (response instanceof HttpServletResponse)
         httpResp = (HttpServletResponse) response;
        StringTokenizer toke = new StringTokenizer(ip, ".");
       
       
        int dots = 0;
        String [] ipByte = new String[4];
   
        while (toke.hasMoreTokens()) {
         ipByte[dots] = toke.nextToken();
         ++dots;
         if(dots == 4) break;
        }
        
        
        if (!isAllowed(ipByte)) {
            httpResp.sendError(HttpServletResponse.SC_FORBIDDEN,"You have no rights to view this page!"+ip);
        } else {
            chain.doFilter(request, response);
        }

 }
   
 private boolean isAllowed(String [] ipByte){
  
  int tempInt=0;
  boolean revalue = false;
    
  
  if(ipByte[0].equals("59") && ipByte[1].equals("73"))
  {
   tempInt = Integer.parseInt(ipByte[2]);
   if(tempInt >= 192 && tempInt <= 223)
    revalue = true;
  }else if(ipByte[0].equals("125") && ipByte[1].equals("222")){
   tempInt = Integer.parseInt(ipByte[2]);
   if(tempInt >= 192 && tempInt <= 223)
    revalue = true;
  }else if(ipByte[0].equals("210") && ipByte[1].equals("47")){
   tempInt = Integer.parseInt(ipByte[2]);
   if(tempInt >= 16 && tempInt <= 31)
    revalue = true;
  }else if(ipByte[0].equals("219") && ipByte[1].equals("217")){
   tempInt = Integer.parseInt(ipByte[2]);
   if(tempInt >= 32 && tempInt <= 47)
    revalue = true;
  }else if(ipByte[0].equals("222") && ipByte[1].equals("27")){
   tempInt = Integer.parseInt(ipByte[2]);
   if(tempInt >= 96 && tempInt <= 127)
    revalue = true;
  }else if(ipByte[0].equals("202") && ipByte[1].equals("198")){
   tempInt = Integer.parseInt(ipByte[2]);
   if(tempInt >= 128 && tempInt <= 143)
    revalue = true;
  }else if(ipByte[0].equals("218") && ipByte[1].equals("62") && ipByte[2].equals("16")){
   tempInt = Integer.parseInt(ipByte[3]);
   if(tempInt >= 32 && tempInt <= 63)
    revalue = true;
  }else if(ipByte[0].equals("218") && ipByte[1].equals("62") && ipByte[2].equals("30")){
   tempInt = Integer.parseInt(ipByte[3]);
   if(tempInt >= 96 && tempInt <= 127)
    revalue = true;
  }else if(ipByte[0].equals("61") && ipByte[1].equals("138") && ipByte[2].equals("177")){
   tempInt = Integer.parseInt(ipByte[3]);
   if(tempInt >= 1 && tempInt <= 127)
    revalue = true;
  }else if(ipByte[0].equals("127") && ipByte[1].equals("0") && ipByte[2].equals("0") && ipByte[3].equals("1"))
  {
   revalue = true;
  }
  
  return revalue;
 }
 
 public void destroy() {

 }

 public String getEncoding() {
  return encoding;
 }
}
将这个servlet放到某个包下,配置web.xml,在

<web-app version="2.4"
 xmlns="http://java.sun.com/xml/ns/j2ee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">后加上

<filter>
<filter-name>EncodingFilter</filter-name>
<filter-class>包的根目录.filter.EncodingFilter</filter-class>
<init-param>
<param-name>Encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>EncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

 

OK了,现在html页的表单都能往数据库中插入中文了。