服务器保存数据乱码解决

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chen_jia_hao/article/details/82844229

当写入数据到数据库中发生乱码时,需要注意:
1.tomcat服务器的配置文件,具体目录在Tomcat安装目录/conf/server.xml文件:
 <Connector URIEncoding="UTF-8" connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" useBodyEncodingForURI="true" />


2.jsp文件的编码格式:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<meta http-equiv="Content-Type"content="text/html;charset=utf-8"/>


3.数据连接配置文件(有点坑),如果是.properties文件要把&amp;改成&,如果是.xml文件要使用&amp,否则写入数据到数据库中会乱码
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url = jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&autoReconnect=true&characterEncoding=utf8


4.安装数据时记得设置指定的编码格式utf8.如果忘记设置可以在安装目录:
C:\Program Files\MySQL\MySQL Server 5.6\中的my.ini文件设置.
具体需要设置2处:
default-character-set=utf8


5.建立数据库时和数据库表时设置成utf8
建立数据库:
CREATE DATABASE `mydb` CHARACTER SET 'utf8' COLLATE 'utf8_general_ci';
 

猜你喜欢

转载自blog.csdn.net/chen_jia_hao/article/details/82844229