如何将 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()
});
}
最佳答案
不幸的是,在将字符串转换为日期时,并没有一种在 RDBMS 之间被广泛接受的标准方法。在您标记的 Postgres 中,您可以使用 to_date
函数:
to_date(?, 'DD/MM/YY')
恕我直言,更好的方法是在 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()
});
}
关于postgresql - 迄今为止的 Java Cast 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37177942/