tags:

views:

46

answers:

1

I have the following method in my java. For some reason when I try and cast l_month as a Date it says that the object can not be cast. In my sql statement I convert the date to just be the month using the DATE_FORMAT with '%M', so it just returns a list of month names and the number of logins for that month. How can I get that l_month field to become a Date. Ultimately I am changing it to a string so if it could be cast to a string that would be great also but nothing seems to be working.

public List<Logins> getUserLoginsByMonth() {
     Session session = getSessionFactory().openSession();
     ArrayList<Logins> loginList = null; 

     try {
          String SQL_QUERY = "SELECT l_date as l_month, SUM(logins) as logins FROM (SELECT DATE_FORMAT(login_time, '%M') as l_date, COUNT(DISTINCT users) as logins FROM user_logins WHERE login_time > DATE_SUB(NOW(), INTERVAL 1 YEAR) GROUP BY DATE(login_time)) AS Z GROUP BY(l_month)";

          Query query = session.createSQLQuery(SQL_QUERY)
                query.addScalar("l_month")
                query.addScalar("logins");

          List results = query.list();
          for(ListIterator iter = results.listIterator(); iter.hasNext() ) {
                Objects[] row = (Object[])iter.next();
                System.out.println((Date)row[0}]);
                System.out.println((BigInteger)row[1]);
          }

     }
     catch(HibernateException e) {
         throw new PersistenceDaoException(e);
     }
     finally {
         session.close();
     }            
}
A: 

DATE_FORMAT is

Blockquote Formats the date value according to the format string.

So it's a string not date.

Dianel Brown