i want a date selection form in jsp and that date variable should be caught in servlet so that i can insert date in mysql. its very urgent plz help me

Dani AI

Generated

Brief, practical steps to capture a date in a JSP, receive it in a servlet, and insert it into MySQL — expanding on the original request from and the comments from and .

A simple, robust flow

  • Put an HTML5 date input on the JSP (browsers send ISO YYYY-MM-DD).
  • In the servlet read request.getParameter("name"), parse to a Java date type, convert to java.sql.* for JDBC, and use a PreparedStatement to insert.
  • Use LocalDate (Java 8+) when possible; fall back to SimpleDateFormat on older JDKs.

Example snippets

JSP:

<form action="InsertDateServlet" method="post">
  <input type="date" name="selectedDate" />
  <input type="submit" value="Save" />
</form>

Servlet (Java 8+):

String dateString = request.getParameter("selectedDate"); // "YYYY-MM-DD"
if (dateString == null || dateString.isEmpty()) { /* handle missing value */ }

LocalDate ld = LocalDate.parse(dateString); // ISO date
java.sql.Date sqlDate = java.sql.Date.valueOf(ld);

try (Connection conn = ds.getConnection();
     PreparedStatement ps = conn.prepareStatement(
         "INSERT INTO mytable (date_column) VALUES (?)")) {
    ps.setDate(1, sqlDate);
    ps.executeUpdate();
}

Older Java (pre-8) parse:

Date util = new SimpleDateFormat("yyyy-MM-dd").parse(dateString);
java.sql.Date sqlDate = new java.sql.Date(util.getTime());
ps.setDate(1, sqlDate);

Key cautions and tips

  • Use PreparedStatement to avoid SQL injection. Do not build SQL by string concatenation.
  • Ensure the DB column type matches your data: DATE for date-only, DATETIME/TIMESTAMP if you need time.
  • input type="date" may need a JS datepicker fallback for old browsers (e.g., jQuery UI datepicker).
  • Handle parse exceptions (DateTimeParseException or ParseException) and nulls gracefully.
  • If using JDBC 4.2+, you can sometimes ps.setObject(1, ld) directly.
  • Note: SimpleDateFormat is not thread-safe — use it per-request (local variable).

This gives a minimal, modern pattern that is safe and portable.

Recommended Answers

All 2 Replies

And? Your question is? No one is going to do it for you.

There are some nice tutorials for of Java section, it is up to you to implement it.
Next time JSP related questions posr in >Web Development>>JSP

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.