java读取mysql数据库的数据中文乱编码你先安装一个mysql的前端工具,如mysqlfront或m

java读取mysql数据库的数据中文乱编码你先安装一个mysql的前端工具,如mysqlfront或m
你先安装一个mysql的前端工具,如mysql front或mysql administrator或navicat,将你要查询的这个数据库的编码改成gb2312,(当然也可以使用命令在mysql控制台中修改,就是麻烦些),然后将你的jsp项目、jsp页面、的编码格式都改成gb2312,这样就没有问题了
这样的话,你直接使用System.out.println(msg);输出内容就是中文了
方法1、可通过过滤器
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class EncodingFilter implements Filter{
private FilterConfig filterConfig;
public void destroy(){

}
public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain)throws IOException,ServletException{
String encoding=filterConfig.getInitParameter("encoding");
request.setCharacterEncoding(encoding);
response.setCharacterEncoding(encoding);
response.setContentType("text/html,charset=UTF-8");
chain.doFilter(request, response);
}
public void init(FilterConfig filterConfig)throws ServletException{
this.filterConfig=filterConfig;
}
}在web.xml 中的配置如下:
encoding
com.handson.control.filter.EncodingFilter

encoding
UTF-8



encoding
/*

方法2:mysql中的 my.ini
default-character-set=gb2312

# SERVER SECTION
# ----------------------------------------------------------------------
#
# The following options will be read by the MySQL Server. Make sure that
# you have installed the server correctly (see above) so it reads this
# file.
#
[mysqld]

# The TCP/IP Port the MySQL Server will listen on
port=3306

#Path to installation directory. All paths are usually resolved relative to this.
basedir="D:/Program Files/MySQL/MySQL Server 5.1/"

#Path to the database root
datadir="C:/Documents and Settings/All Users/Application Data/MySQL/MySQL Server 5.1/Data/"

# The default character set that will be used when a new schema or table is
# created and no character set is defined
default-character-set=gb2312 编码必须一致

方法3、:Mysql语句 show create table XXXXX; 查看当前表使用的编码格式

以上方法可以满足处理乱码
读取数据时应和数据库中的编码一样

← 上一篇: 下一篇: →