【发布时间】:2016-01-09 17:36:19
【问题描述】:
我必须通过 Spring 数据将我的 HashMap<String,Object> 存储到数据库 MySql 中。
要从 HashMap 检索数据,我使用键值,并且可能会发生键不存在的情况,在这种情况下,我必须避免使用 set 方法存储到数据库中,因为我将值转换为 String 或 int 或 float,在这种情况下为 java抛出空异常。
鉴于我有很多行要存储到数据库中,我不想在所有代码行上都使用 If 子句,但我该怎么做呢?
@Override
public void archiveAcquisition(HashMap<String,Object> rowValues, int index) {
switch(index){
case 1:
firstRowValues=rowValues;
break;
case 2:
secondRowValues=rowValues;
break;
case 3:
thirdRowValues=rowValues;
break;
default:
actualRowValues=rowValues;
AvoidNullValueError(ExcelMappingCoordinate.shift,index);
Shift shift=new Shift(actualRowValues.get(ExcelMappingCoordinate.shift.getCoordinate()+index).toString());
shiftServices.create(shift);
MissionProfile missionProfile=new MissionProfile(actualRowValues.get(ExcelMappingCoordinate.missionProfile.getCoordinate()+index).toString());
missionProfileServices.create(missionProfile);
DpfWeighting dpfWeighting=new DpfWeighting();
dpfWeighting.setUnladenWeight((float)actualRowValues.get(ExcelMappingCoordinate.unladenWeight.getCoordinate()+index));
dpfWeighting.setGrossWeight((float)actualRowValues.get(ExcelMappingCoordinate.grossWieght.getCoordinate()+index));
dpfWeighting.setDpfTemperature((float)actualRowValues.get(ExcelMappingCoordinate.dpfTemperature.getCoordinate()+index));
dpfWeightingServices.create(dpfWeighting);
OilSample oilSample=new OilSample();
....
....
例如我做了
if ((value=actualRowValues.get(ExcelMappingCoordinate.unladenWeight.getCoordinate()+index))!=null)
dpfWeighting.setUnladenWeight((float)value);
【问题讨论】:
标签: java mysql exception null spring-data