当前位置: 代码迷 >> Java Web开发 >> jsp连接Mysql乱码解决办法
  详细解决方案

jsp连接Mysql乱码解决办法

热度:1252   发布时间:2013-02-25 21:22:23.0
jsp连接Mysql乱码
我的Mysql是gb2312编码方式的,然后在jsp页面也是用gb2312
但是往数据库里插入后,在数据库里查询时乱码,从数据库里取出来也是乱码
具体应该怎么做啊

------解决方案--------------------------------------------------------
建议全部使用UTF-8....
------解决方案--------------------------------------------------------
连接mysql的url加上&useUnicode=true&characterEncoding=gb2312
------解决方案--------------------------------------------------------

你先用System打印,看查到数据库前有没乱码
你有没接收参数,如果有要用request.setCharacterEncoding()设置
设置页面编码

另外直接修改mysql配置文件my.ini
C:\Program Files\MySQL\MySQL Server 5.1\my.ini
[client]
port=3306
[mysql]
default-character-set=gbk
在utf-8编码下,把该项设成GBk,在mysql客户端就可以直接输入命令,否则要先在mysql客户端设置set names GBK;因为不这样设,命令会被当做utf-8编码,可能会出错。

改变mysql下的编码
[mysqld] 为mysql服务

# 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="C:/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=utf8
这里把编码改成utf8
------解决方案--------------------------------------------------------
楼主只需要做到“四个统一”:mysql数据库编码、数据表编码、中文字段编码及调用页面编码一致,再有乱码都难。强烈建议使用UTF8。
------解决方案--------------------------------------------------------
探讨
连接mysql的url加上&useUnicode=true&characterEncoding=gb2312

------解决方案--------------------------------------------------------
还是 UTF-8 吧,我一直是 UTF-8
------解决方案--------------------------------------------------------
关注
  相关解决方案