在做struts向mysql插入中文記錄時(shí)出現(xiàn) Data too long for column 'name' at row 1 異常,但是插入英文就沒問題,google了下,總結(jié)出下面方法,保證你不會(huì)再出錯(cuò)!
1,mysql的編碼設(shè)置為 utf8 格式,可以從開始菜單那圖形化配置,或者直接進(jìn)入安裝目錄配置my.ini
2,在java連接數(shù)據(jù)庫時(shí)這樣寫:
String url ="jdbc:mysql://localhost/sales?user=root&password=riskfitfeng&useUnicode=true&characterEncoding=utf-8";
3,數(shù)據(jù)庫的編碼為 utf8
4,數(shù)據(jù)庫表編碼為 utf8
5,數(shù)據(jù)庫字段編碼為 utf8
6,jsp頁面中編碼為 utf-8
ref:
http://www.programfan.com/club/showpost.asp?id=19359
頁面顯示中文亂碼的話可以參考這里:
http://hi.baidu.com/lovekaili/blog/item/31830e01c94a2cd3267fb503.html (我很早寫的blog)
write by feng |