漢字亂碼問題體現在:
①:漢字插入到數據庫中,會顯示爲??
②:數據庫中的數據顯示到頁面上的時候,爲??
研究發現,漢字亂碼主要體現在兩個方面
①:數據庫連接url
②:編程獲得configuration的時候。
解決方案
①:編程獲得configuration類的實例的語句爲:
new Configuration().add(XXX.class);
需要的hibernate.properties文件裏數據庫連接url後加上
?characterEncoding=gbk&useUnicode=true;
②:編程時獲得configuration類的實例的語句爲:
new Configuration().configure();
在hibernate.cfg.xml文件的<session-factory/>塊中設置
<property name="connection.useUnicode">true</property>
<property name="connection.characterEncoding">UTF-8</property>
或則寫成
Properties p = new Properties();
p.put("useEncoding","true");
p.put("characterEncoding","utf-8");