Using RowMapper for LocalDate

1k Views Asked by At

I have a class RowMapperFactory that can return an Employee in particular:

new Employee(
                            new BigInteger("7499"),
                            new FullName("JOHN", "ALLEN", "MARIA"),
                            Position.SALESMAN,
                            LocalDate.of(1981, 2, 20),
                            new BigDecimal("1600")
                    )

but I want create a generale form. For new BigInteger, new FullName, Position, new BigDecimal I did it, but how do it for LocalDate. LocalDate must take date from column hired. I try LocalDate.from((TemporalAccessor) resultSet.getDate("hired")), but write that hired column not found. How to solve it?

public class RowMapperFactory {

    public RowMapper<Employee> employeeRowMapper() {
        return new RowMapper<Employee>() {
            @Override
            public Employee mapRow(ResultSet resultSet) throws SQLException {
                return new Employee(
                        new BigInteger(String.valueOf(resultSet.getInt("id"))),
                        new FullName(resultSet.getString("firstname"),resultSet.getString("lastName"),resultSet.getString("middleName") ),
                        Position.valueOf(resultSet.getString("position")),
                        LocalDate.from((TemporalAccessor) resultSet.getDate("hired")),
                        new BigDecimal(resultSet.getInt("salary"))
                );
            }
        };
    }


}
1

There are 1 best solutions below

0
On

If the columnName "hired", is not working, try with index and see if you can find this column you are looking for.

After it is found, you could retrieve it in LocalDate format as such:

 LocalDate date = resultSet.getObject(colIndex, LocalDate.class);