我在web端,有关编码的地方都设置成了utf8数据库连接串也是这样设置的:
jdbc:mysql://localhost:3306/lovedb?useUnicode=true&characterEncoding=utf8数据库表,字段编码都是utf8
但是为什么写到数据库中确变成了gkb?真是怪事啊...为什么我会说是gbk呢?set names gbk;
select * from user_tb;-------------可正常显示set names utf8;
select * from user_tb;-------------乱码..
真是怪事啊

解决方案 »

  1.   


    目前对遇到jsp中文及其乱码问题做个总结,方便自己也方便他人. 主要是5大配置点:
    1 struts2配置 2 数据库 3 页面 4 jdbc连接 5 tomcat
    以下解决方法不分顺序,大家按照自己的配置对比.
    1 struts2配置
     struts2在配置struts.properties文件中加上struts.i18n.encoding=UTF-8 或者在struts.xml里加上<constant name="struts.i18n.encoding" value="UTF-8" /> 有的朋友说加上struts.locale=zh_CN,但是我的struts.locale为en_GB没有问题.
    2 数据库
    2.1 mysql的表的编码方式,其DDL应该为: ENGINE=InnoDB DEFAULT CHARSET=utf8; 可以使用show create table tbname查看.
    2.2 安装mysql数据库时就应该以utf8编码默认安装查看mysql数据库默认编码方式可以在其目录下my.ini文件中找到mysql和mysqld下default-character-set值为utf8或者在mysql数据库命令行下以"\s;"查看.
    3 页面
    每个页面上加上<%@page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8" %> 这样在jsp页面里,点右键,查看编码方式则为UTF-8.
    4 jdbc连接
     设置characterencoding为UTF-8 如jdbc.mysql.url=jdbc:mysql://localhost:3306/db?useUnicode=true&amp;characterEncoding=UTF8
    5 tomcat
    为了保证get/post数据都采用相同的UTF8编码,我们在server.xml中进行了如下设置: <Connector port="8080" protocol="HTTP/1.1"                connectionTimeout="20000"                redirectPort="8443"  URIEncoding="UTF-8" />
    其中,当当使用IIS作为webserver转发servlet/jsp请求时IIS是通过AJP协议,把请求转发到Tomcat监听的8009端口,所以需要配置端口8009的URIEncoding为UTF-8.
         msyql数据库安装时候需要默认编码方式或者使用MySQL Server Instance Config Wizard 配置,
    在设置Configure the MySQL Server 5.0 server instance那里的时候选择Manual Selected Default Character Set / Collation 为 UTF8 而不是默认的 LATIN1 在MYSQL里查看得 mysql> \s;
    -------------- E:\MySQL\MySQL Server 5.0\bin\mysql.exe  Ver 14.12 Distrib 5.0.51a, for Win32 (ia32) Connection id:          2 Current database: Current user:          root@localhost SSL:                    Not in use Using delimiter:        ; Server version:        5.0.51a-community-nt MySQL Community Edition (GPL) Protocol version:      10 Connection:            localhost via TCP/IP Server characterset:    utf8 Db    characterset:    utf8 Client characterset:    utf8 Conn.  characterset:    utf8 TCP port:              3306 Uptime:                25 sec Threads: 1  Questions: 4  Slow queries: 0  Opens: 12  Flush tables: 1  Open tables: 6  Queries per second avg: 0.160
        my.ini文件内容为
    [mysql] default-character-set=utf8 # 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="E:/MySQL/MySQL Server 5.0/" #Path to the database root datadir="E:/MySQL/MySQL Server 5.0/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=utf8 ...........
      

  2.   

    其实,mysql的编码是分层的,第一层,数据库编码,然后是数据库表,之后是数据库表字段,你要查看具体编码是什么,可以用show命令,例如,查看表的编码,用show create table tableName,这样就可以看到是什么编码了,你出现这样的问题,看看是不是每一层都是同一个编码
      

  3.   

    SET NAMES x
    相当于
    SET character_set_client = x;
    SET character_set_results = x;
    SET character_set_connection = x;是用来设置客户端的字符集的,与服务器端的字符集无关
      

  4.   

     SET NAMES indicates what character set the client will use to send SQL statements to the server. Thus, SET NAMES 'cp1251' tells the server “future incoming messages from this client are in character set cp1251.” It also specifies the character set that the server should use for sending results back to the client. (For example, it indicates what character set to use for column values if you use a SELECT statement.)A SET NAMES 'x' statement is equivalent to these three statements:SET character_set_client = x;
    SET character_set_results = x;
    SET character_set_connection = x;引自: MySQL 5.1 Reference Manual 
    http://http://dev.mysql.com/doc/refman/5.1/en/charset-connection.html
      

  5.   

    到MySql的安装目录下 找到 my.ini文件,用Editplus 打开,找到57行和81行,把那的编码格式改成GBK.
      

  6.   

    建数据库和表的时候,也要建 成utf-8的,mysql默认是gbk的
      

  7.   

    如果你都用utf-8的话
    那你将mysql安装目录下的my.ini中的两个编码(一个是mysql内部编码,一个是接收用户输入编码)都改成utf-8
    然后
    注意,要重新建库和建表