在进行hql语句查询的时候,由于是面向对象的查询语句,所以与sql+jdbc有所不同。
在数据库中,我的报警字段alarmtime是字符串的类型,直接用字符串类型来查询一段时间内的数据显然不行,所以我一开始想的就是将
字符串转换为时间类型。在sql中:
mysql> select alarm_grade , alarm_time from tb_sys_alarm_info where alarm_grade
=1 and date_format(alarm_time,'%Y-%m-%d %h:%i:%s') <now();
可以得到结果。(我用的是mysql)
但用hql查询的时候却报错了。说不能解析我的sql语句,后来才明白,date_format();函数是将一个字符串转换为时间类型,所以第一个参数必须加上‘’。
查询的条件变为:"select * from Sdingalrm where alarmGrade=3 and date_format(alarmTime,'%Y-%m-%d %h:%i:%s') between date_format('"+time1+"','%Y-%m-%d %h:%i:%s') and date_format('"+time2+"','%Y-%m-%d %h:%i:%s')",其中time1和time2是页面选择的时间。
如此,查询成功!!
附:个人qq空间:http://user.qzone.qq.com/449125551/profile/sync#!app=2&via=QZ.HashRefresh&pos=1384014434