I'm using SuperCsv to parse a csv file into Javabeans which I then save in a DB table. I get the following exception when trying to input the date column. After reading the superscv API and documetation I really can't understand how to correctly write a csv to a Javabean and persist the same to a datatbase. The exception:
org.supercsv.exception.SuperCsvCellProcessorException: the input value should be of type java.lang.String but is java.util.Date processor=org.supercsv.cellprocessor.ParseDate context={lineNo=2, rowNo=2, columnNo=3, rowSource=[moredata, 450, Thu Jan 01 00:09:00 PST 2015]}
The csv file:
someData,23,01/09/2015
moredata,450,01/09/2015
evenMoreData,500,01/09/2015
I'm parsing the CSV as follows:
private List<Timeseries> readCSVToBean() throws IOException {
ICsvBeanReader beanReader = null;
List<Timeseries> timeSeries = new ArrayList<Timeseries>();
try {
beanReader = new CsvBeanReader(new FileReader(pathName),
CsvPreference.STANDARD_PREFERENCE);
// the name mapping provide the basis for bean setters
final String[] nameMapping = new String[]{"varval", "actualNum", "daterec"};
//just read the header, so that it don't get mapped to Timeseries object
final String[] header = beanReader.getHeader(false);
final CellProcessor[] processors = getProcessors();
Timeseries timeEntity;
while ((timeEntity = beanReader.read(Timeseries.class, nameMapping,
processors)) != null) {
timeSeries.add(timeEntity);
}
} finally {
if (beanReader != null) {
beanReader.close();
}
}
return timeSeries;
}
private static CellProcessor[] getProcessors() {
final CellProcessor[] processors = new CellProcessor[]{
new NotNull(), // varval
new ParseInt(), // actualNum
//new FmtDate("dd/MM/yyyy")
new ParseDate("dd/mm/yyyy") // Daterec
};
return processors;
}
The Timeseries
bean is:
public class Timeseries implements Serializable {
private static final long serialVersionUID = 1L;
private Integer timid;
private String varval;
private Integer actualnum;
private Date daterec;
// ...
}
As documented on the Super CSV website, ParseDate
is used for reading (String
-> Date
) and FmtDate
is used for writing (Date
-> String
).
You'll need one set of processors for reading, and another for writing.