struts+hibernate+mysql HQL语句查询带中文的查询不出数据的解决方法
HQL语句:
String queryString = "from ViewKk where "
+ "overTime" + "<= ? and "+"amount"+">0 and overTime>'"
+GetTime.getStringDate4()+"' and areaName=:name order by overtime desc" ;
语句本身没有问题。一开始以为HQL语句有问题,我就把判断语句一个一个的测试发现overTime、amount都没问题,就areaName进行中文判断时就有问题了。我的情况如下:
jsp的Encoding为:GBK
Tomcat的Encoding为:GBK
数据库的Encoding为:GBK
还是无法查询出数据。最后在网上找到一前辈的方法,就能解决了。
方法如下:
打开hibernate的配置文件:hibernate.cfg.xml
然后找到:
<property name="connection.url">
jdbc:mysql://127.0.0.1:3306/GD
</property>
将其修改为:
<property name="connection.url">
jdbc:mysql://127.0.0.1:3306/GD?useUnicode=true&characterEncoding=gbk
</property>