JJ Codo
JJ Codo

Reputation: 9

Java JDBC how to pass date in

How can I take the the date value from my html, then add it to the database.

I tried many ways but its not working. I keep getting back a NULL value in the database or an error message "java.text.dateformat.parse(unknown source)"

HTML:

Date Of Birth: <input type="date" name="dob">

JAVA:

String date = request.getParameter("dob");


Date dob1 = new SimpleDateFormat("MM/dd/yyyy"));

 PreparedStatement createUser = connection.prepareStatement(
                  "INSERT into users (dob1)" +
                 " VALUES ( ?)");{


                 createUser.setDate(1, (java.sql.Date) dob1);


                 int newUser = createUser.executeUpdate(); 
 }

Upvotes: 0

Views: 1438

Answers (1)

Mark Sholund
Mark Sholund

Reputation: 1312

String date = request.getParameter("dob");


Date dob1 = new SimpleDateFormat("MM/dd/yyyy"));

This second line is wrong. Change to

Date dob1 = new SimpleDateFormat("MM/dd/yyyy")).parse(date);

edit

By the comment you're using the wrong date format. Use it instead.

Date dob1 = new SimpleDateFormat("yyyy-MM-dd")).parse(date);

Upvotes: 1

Related Questions