如何将java.lang.String转换为java.sql.Date以获得下列值:'12/03/2016'
我试过这个:
@Override
public int insertNewApplication(StudenDetails sd)
{
String sql = "INSERT INTO student_details"
+ "(applicant_name, applsex, "
+ "designation, date_of_superannuation, "
+ "ou_code, residential_address, "
+ "phone_no, mobile_no,email_id,token_user_id,token_password, "
+ "staff_status,office_type_code) VALUES (?, ?, ?,to(?,'DD/MM/YYYY'), ?, ?, ?, ?, ?, ?, ?,?,?)";
return jdbcTemplate.update(sql, new Object[] {
tpd.getApplicant_name(), tpd.getApplsex(),tpd.getDesignation(),tpd.getDate_of_superannuation(),tpd.getOu_code(),tpd.getResidential_address(),tpd.getPhone_no(),tpd.getMobile_no(),
tpd.getEmail_id(),tpd.getToken_user_id(),tpd.getToken_password(),tpd.getStaff_status(),tpd.getOffice_type_code()
});
}发布于 2016-05-14 16:49:25
不幸的是,在RDBMS之间没有一种广泛接受的将字符串转换为日期的标准方法。在您标记的Postgres中,可以使用to_date函数:
to_date(?, 'DD/MM/YY')一种更好的方法,IMHO,是用Java进行这种转换,使您的应用程序更容易移植:
private static final SimpleDateFormat FORMAT_CONVERTER =
@Override
public int insertNewApplication(StudenDetails sd)
{
String sql = "INSERT INTO student_details"
+ "(applicant_name, applsex, "
+ "designation, date_of_superannuation, "
+ "ou_code, residential_address, "
+ "phone_no, mobile_no,email_id,token_user_id,token_password, "
+ "staff_status,office_type_code) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?,?)";
Date date = FORMAT_CONVERTER.parse(tpd.getDate_of_superannuation());
return jdbcTemplate.update(sql, new Object[] {
tpd.getApplicant_name(), tpd.getApplsex(),tpd.getDesignation(),date,tpd.getOu_code(),tpd.getResidential_address(),tpd.getPhone_no(),tpd.getMobile_no(),
tpd.getEmail_id(),tpd.getToken_user_id(),tpd.getToken_password(),tpd.getStaff_status(),tpd.getOffice_type_code()
});
}https://stackoverflow.com/questions/37177942
复制相似问题