JSP MYSQL Date Val

Hi, i am creating an addressbook that uses a mysql database to store the data. I input peoples birthday in the format yyyy-mm-dd. the mysql birthday field is set to date so it will only accept that format.how can i validate the date so only the correct format is entered?
[285 byte] By [symbola] at [2007-10-2 14:30:47]
# 1

If you use a prepared statement, and a java.sql.Date object, then the standard format of the date that the database uses is irrelevant.

You then use a java.text.SimpleDateFormat to convert a String to a date.

You can also use it to validate whatever format you like.

Instead of

sql = "Insert into birthday(date) values ('" + birthDay +"')";

have

String sql = "Insert into birthday(date) values (?)";

String sDate = ?

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // or whatever format you want

java.util.Date birthDate = null;

try{

birthDate = sdf.parse(sDate);

}

catch(ParseException e){

// an error parsing the date. Send an error message back to the client

}

java.sql.Date sqlDate = new java.sql.Date(birthDate.getTime());

PreparedStatement stmt = connection.prepareStatement(sql);

stmt.setDate(1, sqlDate);

stmt.execute();

evnafetsa at 2007-7-13 12:52:43 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...