I have to retrieve a Date from an oracle database and store it into a java object as localDate. This is the code I have tried:
public Sample findSampleById(Integer sampleId) {
// TODO Auto-generated method stub
PreparedStatement pStmt = null;
ResultSet rs = null;
Sample sample = new Sample();
String sql = "select * from sample where sampleid =" + sampleId;
try {
pStmt = getConnection().prepareStatement(sql);
rs = pStmt.executeQuery();
while(rs.next()) {
sample.setSampleId(rs.getInt("sampleid"));
sample.setSampleKindId(rs.getInt("samplekindid"));
sample.setExpirationDate(rs.getDate("expirationdate").toInstant().atZone(ZoneId.systemDefault()).toLocalDate());
}
} catch (SQLException e) {
throw new DataException(e);
}
return sample;
}
Instead of this sample.setExpirationDate(rs.getDate("expirationdate").toInstant().atZone(ZoneId.systemDefault()).toLocalDate());
I have also tried this
java.sql.Date retrieved = java.sql.Date.valueOf(rs.getString("expirationDate")); sample.setExpirationDate(retrieved.Date());
Sample is a class with int sampleId, int sampleKindId and LocalDate expirationDate
My JUnit Test for the method tells me in the line where I ask for the expiration date that it throws an exception. Do I do anything wrong with the conversion?