1、首先我们通过cmd
确认mysql
数据库是否已经设置字符集为utf-8
2、如果mysql
数据库未设置成utf-8
:
找到mysql
安装路径下的my.ini
配置文件,进行修改
【my.ini】
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.
[client]
default-character-set=utf8
[mysqld]
default-storage-engine=INNODB
character-set-server=utf8
collation-server=utf8_general_ci
basedir=F:\mysql-5.7.37-winx64\mysql-5.7.37-winx64
datadir=F:\mysql-5.7.37-winx64\mysql-5.7.37-winx64\data
port=3306
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
max_allowed_packet=16M
[mysql]
default-character-set=utf8
其中:
basedir
是mysql
的安装路径datadir
是mysql
的存放数据的路径,一般是安装目录下的data
文件夹
修改完my.ini
,要重启mysql
。
3、如果mysql
数据库已经设置成utf-8
:
- 可以先在
servlet
中获取参数的地方使用System.ou.println()
打印出获取的参数,若此刻输出的参数内容是乱码,可以认定从页面传来的内容和我们当前获取的内容编码不一致 - 在
serlvet
中添加一行代码:
request.setCharacterEncoding("UTF-8");
- 现在再试一下,应该就可以解决这个问题了