javaapache-commons-dbutils

Getting a List of Dates from Apache Commons DBUtils


I am trying to get a list of dates from a table using Apache Commons DBUtils. This kind of query works well with Strings and my own POJOs, but for some reason does not work on java.sql.Date's.

CALENDAR_DATE in PUBLIC_HOLIDAYS is a valid date field.

Query :

select CALENDAR_DATE TIME from PUBLIC_HOLIDAYS where CALENDAR_DATE between ? and ?

Java code

ResultSetHandler<List<java.sql.Date>> h = new BeanListHandler<>(java.sql.Date.class);
QueryRunner db = new QueryRunner(dataSource,true);
List<java.sql.Date> sqlDates = db.query(sql, h,new java.sql.Date(from.getTime()),new java.sql.Date(to.getTime()));

Solution

  • The answer is to build your own ResultSetHandler. Maybe there is a simpler way, but I could not find one.

    ResultSetHandler<List<java.sql.Date>> h = new ResultSetHandler<List<java.sql.Date>>() {
    
    public List<java.sql.Date> handle(ResultSet rs) throws SQLException {
      List<java.sql.Date> result = new ArrayList<java.sql.Date>();
      while (rs.next()) {
         result.add(rs.getDate(1));
      }
      return result;
    }
    };
    
    List<java.sql.Date> sqlDates = db.query(sql, h,new java.sql.Date(from.getTime()),new java.sql.Date(to.getTime()));