天天看點

java向mysql資料庫插入資料中文亂碼

遇到一個java項目向背景插入資料時,mysql資料庫内中文亂碼的問題,如圖

java向mysql資料庫插入資料中文亂碼

然後開始排查逐漸排查

資料庫表是UTF8

java向mysql資料庫插入資料中文亂碼

前段是UTF-8

java向mysql資料庫插入資料中文亂碼

idea是UTF-8

java向mysql資料庫插入資料中文亂碼

JAVA是utf-8

java向mysql資料庫插入資料中文亂碼

web.xml配了utf-8

<!-- 編碼Filter 加載配置 Start -->
	<filter>
		<filter-name>SetCharacterEncoding</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>SetCharacterEncoding</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

           

最後修改了linux的mysql資料庫為utf-8後重新開機,解決了問題

修改檔案 vim etc/my.cnf檔案

#做了如下添加
[client]
default-character-set=utf8

character-set-server=utf8

init_connect='SET NAMES utf8'
           

重新開機mysql

service mysqld restart
           
java向mysql資料庫插入資料中文亂碼

最後再向mysql插資料就不亂碼了