問題描述及解決方案
使用MyBatis操作資料庫一直沒問題,使用了Java Properties檔案存放訪問資料庫的相關屬性后,往資料庫添加資料卻出現了中文亂碼,經排查,問題出在訪問資料庫的url上,我在xml檔案中的url如下所示:
jdbc:mysql://localhost/college?useUnicode=true&characterEncoding=utf8
請注意,這里面的轉義字符“&”代表字符“&”,是xml和html中的用法,我把這個url直接復制到了Java Properties檔案中,但是在Properties檔案中,是不認這個轉義字符的,因此,后面的“characterEncoding=utf8”就無效了,自然會出現亂碼,
找到原因后,解決方案也很簡單,在Java Properties檔案中不需要使用轉義字符“&”,直接使用“&”即可,如下所示:
jdbc:mysql://localhost/college?useUnicode=true&characterEncoding=utf8
擴展知識
Java Properties檔案不能輸入中文的問題
在eclipse中將Java Properties檔案的默認編碼由ISO-8859-1改為UTF-8即可,點擊Window->Preferences打開如下視窗進行設定:

property-placeholder讀取Java Properties檔案里的中文的方法
設定file-encoding=“utf8”,如下代碼所示:
<context:property-placeholder location="classpath:db.properties" file-encoding="utf8"/>
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/3390.html
標籤:其他
上一篇:堪比培訓機構的MySQL系列博客
下一篇:MySql語法及命令(用心整理)
