
hi i am using servlets i have a problem in doing an application.
in my application i have html form, in which i have to insert on date value, this date value is retrieved as a request parameter in my servlet. and it was in the form of string. so my question is, how to convert that string value into java.sql.Date type and insert into database

Servlet Insert Date
1)date.html:
<html> <form method="post" action="http://localhost:8080/examples/InsertDate"> Enter date:<input type="text" name="dd"><br> <input type="submit" action="Submit"> </form> </html>
2)InsertDate.java:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
public class InsertDate extends HttpServlet {
public void doPost(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
String d=req.getParameter("dd");
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
PreparedStatement pstmt = con.prepareStatement("insert into emp(JOINING_DATE) values(?)");
java.sql.Date sqlDate = new java.sql.Date(new java.util.Date(d).getTime());
pstmt.setDate(1, sqlDate);
pstmt.executeUpdate();
out.println("Date is successfully inserted");
}
catch(Exception e){
System.out.println(e);
}
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.