This is the test code,first use hibernate ,then use the native sql:
Code:
@Test
public void testSQLAndHibernate() throws ParseException {
start = sdf.parse("2011-02-22 10:00:00");
end = sdf.parse("2011-02-22 16:00:00");
// use hibernate
Session sess = HibernateUtil.getSessionFactory().getCurrentSession();
sess.beginTransaction();
Query q = sess
.createQuery(
"select log.uri,count(log.uri) as num from LogEntry log where log.time between ? and ? group by log.uri order by num desc")
.setDate(0, start).setDate(1, end);
System.out.println("get "+q.list().size()+" results by hibernate");
System.out.println("++++++++++++++");
// use sql
SimpleDateFormat sdf_sql = new SimpleDateFormat("yyyyMMddHHmmss");
String sql = "select uri,count(uri) as num from t_log where time between "
+ sdf_sql.format(start) + " and " + sdf_sql.format(end)
+ " group by uri order by num desc";
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db",
"root", "0000");
Statement state = conn.createStatement();
ResultSet rs = state.executeQuery(sql);
int i=0;
while (rs.next()) {
//System.out.println(rs.getString(1) + " " + rs.getLong(2));
i++;
}
System.out.println("get "+i+" results by sql query");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
OutPut:
get 0 results by hibernate
++++++++++++++
get 24 results by sql query
Also from the hibernat log I get :
Code:
binding parameter [1] as [DATE] - Tue Feb 22 10:00:00 CST 2011
binding parameter [2] as [DATE] - Tue Feb 22 16:00:00 CST 2011
It seems that the start and end date are correct, so I wonder if the format is right or not?