1

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"))
                );
            }
        };
    }


}
Richard Walker
  • 133
  • 2
  • 10
  • by checking if that column exists, and whether or not you are using the right datatype – Stultuske Dec 15 '21 at 10:26
  • but column exists? How to do it – Richard Walker Dec 15 '21 at 10:29
  • 'but column exists', not according to that error message. That problem is not related to the mapping, it specifically tells you: "hired column not found" – Stultuske Dec 15 '21 at 10:30
  • What data type does this column hold? Are you sure it is a `TemporalAccessor`? The error message definitely tells you the column could not be found, you may have a typo somewhere else... – deHaar Dec 15 '21 at 10:33
  • this column hold LocalDate data types format "1980-12-17". it is not a TemporalAccessor – Richard Walker Dec 15 '21 at 10:39
  • but how else to access the data? – Richard Walker Dec 15 '21 at 10:40
  • if it's not a TemporalAccessor, why are you trying to parse it to one? – Stultuske Dec 15 '21 at 10:54
  • So it most likely holds formatted `String`s and no `java.sql.Date`s (which is what you get when you call `getDate(...)` on a `ResultSet`) and that would also explain why the column cannot be found: There is no column named `'hired'` that holds DATE or DATETIME values. Try `LocalDate.parse(resultSet.getString("hired")` or if it really returns a `Date`, then convert it via `Instant`, but then you really need the `getDate(..)` call. – deHaar Dec 15 '21 at 10:56

1 Answers1

0

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);
JCompetence
  • 6,997
  • 3
  • 19
  • 26