java读取mysql数据库的数据中文乱编码
发布网友
发布时间:2022-04-08 00:33
我来回答
共3个回答
热心网友
时间:2022-04-08 02:03
你先安装一个mysql的前端工具,如mysql front或mysql administrator或navicat,将你要查询的这个数据库的编码改成gb2312,(当然也可以使用命令在mysql控制台中修改,就是麻烦些),然后将你的jsp项目、jsp页面、的编码格式都改成gb2312,这样就没有问题了
这样的话,你直接使用System.out.println(msg);输出内容就是中文了
热心网友
时间:2022-04-08 03:21
方法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 中的配置如下:<filter>
<filter-name>encoding</filter-name>
<filter-class>com.handson.control.filter.EncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
方法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; 查看当前表使用的编码格式
以上方法可以满足处理乱码
热心网友
时间:2022-04-08 04:55
读取数据时应和数据库中的编码一样