org.supercsv.exception.SuperCsvCellProcessorException:输入值应为 java.lang.String 类型,但为 java.util.Date
org.supercsv.exception.SuperCsvCellProcessorException: the input value should be of type java.lang.String but is java.util.Date
我正在使用 SuperCsv 将 csv 文件解析为 Javabeans,然后将其保存在数据库中 table。尝试输入日期列时出现以下异常。在阅读了 superscv API 和文档后,我真的不明白如何正确地将 csv 写入 Javabean 并将其保存到数据库中。异常:
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]}
csv 文件:
someData,23,01/09/2015
moredata,450,01/09/2015
evenMoreData,500,01/09/2015
我正在按如下方式解析 CSV:
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;
}
Timeseries
bean 是:
public class Timeseries implements Serializable {
private static final long serialVersionUID = 1L;
private Integer timid;
private String varval;
private Integer actualnum;
private Date daterec;
// ...
}
如 Super CSV website 中所述,ParseDate
用于读取(String
-> Date
),FmtDate
用于写入(Date
-> String
).
您需要一组处理器用于读取,另一组用于写入。
我正在使用 SuperCsv 将 csv 文件解析为 Javabeans,然后将其保存在数据库中 table。尝试输入日期列时出现以下异常。在阅读了 superscv API 和文档后,我真的不明白如何正确地将 csv 写入 Javabean 并将其保存到数据库中。异常:
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]}
csv 文件:
someData,23,01/09/2015
moredata,450,01/09/2015
evenMoreData,500,01/09/2015
我正在按如下方式解析 CSV:
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;
}
Timeseries
bean 是:
public class Timeseries implements Serializable {
private static final long serialVersionUID = 1L;
private Integer timid;
private String varval;
private Integer actualnum;
private Date daterec;
// ...
}
如 Super CSV website 中所述,ParseDate
用于读取(String
-> Date
),FmtDate
用于写入(Date
-> String
).
您需要一组处理器用于读取,另一组用于写入。