Hello aghori,
Ideally you should first convert the string into java.util.Date and then you should use prepared statement to insert the date. The following snippet should help you understand this. It assumes that you already know the format in which the string date is captured. For the below code snippet I am assumes ISO date format.
Date dtToday = null;
DateFormat dtFmt = null;
PreparedStatement pstmt = null;
try {
dtFmt = new SimpleDateFormat("yyyy-MM-dd");
dtToday = dtFmt.parse(strDate);
pstmt = con.prepareStatement("INSERT INTO tblfoo(first_name, last_name, dob) VALUES(?, ? ?)");
pstmt.setString(1, strFirstName);
pstmt.setString(2, strLastName);
pstmt.setDate(3, dtToday);
pstmt.execute();
} catch (ParseException ex) {
log.error("Entered date {} has invalid format, expected yyyy-MM-dd", new Object[] {strDate});
}
If you want to use the date literal then use following syntax.
DATE '1998-12-25'
More information on this can be found in
Oracle Documentation[
^]. With Date literal the above code can be rewritten as shown below. However the string date must be in ISO Date format.
Statement stmt = null;
stmt = con.createStatement();
stmt.execute("INSERT INTO tblfoo(first_name, last_name, dob) VALUES('"
.concat(strFirstName).concat("', '")
.concat(strLastName).concat("', DATE '")
.concat(strDate).concat("')"));
Note - The above mentioned method is susceptible to SQL Injection attack and hence is not a preferred way.
Regards,