日期:2014-05-16  浏览次数:20841 次

MySQL中文问题求救!!!

?

症状:??中文乱码

尝试过的方法:

一、试过修改my.ini?? default-character-set=latin1 ===>> gbk失败

二、增加servlet过滤器 jsp页面设置pageEncoding="GB2312" 无效

三、web开发时来自表单的POST数据,不转码直接入DB Url:

String url = "jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=GBK"

?

页面显示时,用new String(dd.getBytes("iso-88-59-1"),"GB2312")转一下可以正确显示从DB中读出的中文数据

但是纯java读取控制台显示就失败,比如java在xml和DB间传递中文数据

四、努力的思考:java中都是Unicode编码,Tomcat是iso-8859-1编码,Mysql默认是latin1?不想还可以,越想越乱

五、google搜索结果:不同的情况不同的答案,以后面试我也问Mysql中文问题,挺有意思的哈

?

环境:Mysql5。0 Tomcat5.5 Jdk1.5 Windows 2003Server,Jbuilder2006

?

数据库:mysql5字符集编码:

query result(8 records)

Variable_nameValue
character_set_client latin1
character_set_connection latin1
character_set_database latin1
character_set_filesystem binary
character_set_results latin1
character_set_server latin1
character_set_system utf8
character_sets_dir C:\Program Files\MySQL\MySQL Server 5.0\share\charsets\

?


?

//数据库url
String url = "jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=GBK"
String mySqlDriver = "org.gjt.mm.mysql.Driver";

DAO部分
public ArrayList GetAllInfo() {
        ArrayList aMovies = new ArrayList();
        Connection conn = null;
        Statement stmt = null;
        try {
            conn = new DbConnection().GetConnection();
            stmt = conn.createStatement();
            String sql = "select * from movie";
            ResultSet rs = stmt.executeQuery(sql);
            while (rs.next()) {
                movie m = new movie();
                m.setId(rs.getInt("id"));
                m.setMoviename(rs.getString("moviename").trim());
                m.setStars(rs.getString("stars").trim());
                aMovies.add(m);
            }
        } catch (Exception ex) {
            System.err.println(ex.getMessage());
            ex.printStackTrace();
        } finally {
            try {
                stmt.close();
                conn.close();
            } catch (SQLException ex1) {
                ex1.printStackTrace();
            }
        }
        return aMovies;
    }

数据显示页页面(向数据库中插入数据时,不转码,取出jsp显示时,用new String(dd.getBytes("iso-88-59-1"),"GB2312")没有问题,但是换成用struts logic 和bean标签输出 的时候,用不了上述函数,所以从DB中出来的中文乱码,servlet过滤器也加上了,失败)
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ page contentType="text/html; charset=GB2312" pageEncoding="GB2312"  %>
<html>
<head>
<title>
sucess
</title>
</head>
<body bgcolor="#ffffff">

<table border="1">
<tr>
<td>moviename</td>
<td>stars</td>
</tr>
<logic:iterate id="mv" scope="request" name="movies" >
<tr>
<td><bean:write name="mv" property="moviename" filter="true"/></td>
<td><bean:write name="mv" property="stars"/></td>
</tr>
</