我想读取Excel日期值并将其存储到我的数据库中。如何读取Excel日期并将其存储到我的数据库中?
下面的代码是我的本钱:
Date birthdate = null;
case 8:
birthdate = cell.getDateCellValue();
break;
的开关情况的日期存储在一个变量,并把它变成一个数组之后。
Identity id = new Identity(BSN, sort, birthdate, place);
我将这些值的多个值存储到数组列表中。
for(Identity id : Identities) {
System.out.println(id.toString());
idmc.insertID(""+id.getBSN(), id.getSort(), id.getBirthdate(), id.getPlace());
}
日期的getter和setter很明显,但我会发布它以防万一。
public void Birthdate (Date birthdate) {
this.birthdate = birthdate;
}
public Date getBirthdate() {
return birthdate;
}
然后插入我从阵列获取值,我用这个方法:
public String insertID(String BSN, String SoortID, Date UitgiftedatumID, String UitgifteplaatsID) {
String returning = null;
try {
query = "insert into Identiteit values(?,?,?,?);";
pst = connection.prepareStatement(query);
pst.setInt(1, Integer.parseInt(BSN));
pst.setString(2, Sort);
pst.setDate(3, birthdate);
pst.setString(4, place);
int response = pst.executeUpdate();
returning = response +" Records has/have been edited";
} catch (SQLException e) {
returning = " ";
}
return returning;
}
然而,该行:pst.setDate(3, UitgiftedatumID);
说:Incomatible types: java.util.Date cannot be converted to java.sql.Date.
我已经试过像铸造: pst.setDate(3, (java.sql.Date) UitgiftedatumID);
但不幸的是,这并没有为我工作。
相反初始化现在我使用的代码中的日期。然而,'.getTime'部分给出了一个评论:'取消引用空指针',所以我得到了一个'NullPointerException'的规则,我初始化'java.sql.Date sqlDate = new java.sql.Date(UitgiftedatumID .getTime());'this。 – Wout
对不起,我错过了初始日期可能为'null'的可能性。我已经更新了我的答案来处理这个问题。 –
非常感谢你非常非常的人!它现在有用!最后,日期显示在我的数据库中。非常感谢你,祝你有美好的一天! – Wout