汉字乱码问题体现在:
①:汉字插入到数据库中,会显示为??
②:数据库中的数据显示到页面上的时候,为??
研究发现,汉字乱码主要体现在两个方面
①:数据库连接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");