首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > Mysql >

struts2连mysql乱码解决办法

2012-08-02 
struts2连mysql乱码解决方法转载2 数据库2.1 mysql的表的编码方式,其DDL应该为: ENGINEInnoDB DEFAULT CH

struts2连mysql乱码解决方法
转载

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 页面

每个页面上加上<%@pagepageEncoding="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那里的时候选择ManualSelected Default Character Set / Collation 为 UTF8 而不是默认的 LATIN1在MYSQL里查看得 mysql> \s;

-------------- E:\MySQL\MySQL Server5.0\bin\mysql.exe? Ver 14.12 Distrib 5.0.51a, forWin32 (ia32) Connection id:? ?? ? ? 2 Currentdatabase: Current user:? ?? ? ?root@localhost SSL:? ?? ? ?? ? ?? ? Not in use Usingdelimiter:? ? ?? ; Server version:?? ? ?5.0.51a-community-nt MySQL Community Edition (GPL) Protocolversion:? ? ? 10Connection:? ? ?? ? ? localhostvia TCP/IP Servercharacterset:? ? utf8Db? ?characterset:? ? utf8 Clientcharacterset:? ? utf8Conn.? characterset:?? utf8 TCP port:?? ? ?? ? ? 3306Uptime:? ? ?? ? ?? ? 25 sec Threads:1? Questions: 4? Slow queries:0? Opens: 12? Flush tables:1? Open tables: 6? Queries persecond avg: 0.160

???my.ini文件内容为

[mysql] default-character-set=utf8 # SERVERSECTION #----------------------------------# # The following options will be read by the MySQL Server. Makesure that # you have installed the server correctly (see above) soit reads this # file. # [mysqld] # The TCP/IP Port the MySQL Serverwill listen on port=3306 #Path to installation directory. All pathsare usually resolved relative to this. basedir="E:/MySQL/MySQLServer 5.0/" #Path to the database root datadir="E:/MySQL/MySQLServer 5.0/Data/" # The default character set that will be usedwhen a new schema or table is # created and no character set isdefined default-character-set=utf8...........

热点排行