[英]How to Store date from Html form to database using java?
I have inputted the date through html page and trying to Store it to database but i got this exception java.lang.NumberFormatException: For input string: "09/12/2016" and i am also getting an error in netbeans "to surround Date date = (Date)formatter.parse(pswd);
with try
and catch
" here is my code: 我已经通过html页面输入了日期,并尝试将其存储到数据库中,但是我遇到了此异常java.lang.NumberFormatException:对于输入字符串:“ 09/12/2016”,我在netbeans中也遇到了一个错误“包围Date date = (Date)formatter.parse(pswd);
使用try
and catch
“这是我的代码:
public class OfferRide extends HttpServlet {
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String status = "";
try {
String userName = req.getParameter("Source");
String email = req.getParameter("Destination");
String pswd = req.getParameter("Date");
SimpleDateFormat formatter = new SimpleDateFormat("YYYY-MM-DD");
java.sql.Date date = (java.sql.Date)formatter.parse(pswd);
String id = req.getParameter("Seats");
int seat =Integer.parseInt(id);
String id1= req.getParameter("Phone");
long phone = Long.parseLong(id1);
Connection con = DataBaseServices.getConnection();
PreparedStatement ps = con.prepareStatement("insert into offerride values(?,?,?,?,?)");
ps.setString(1, userName);
ps.setDate(2, date);
ps.setString(3, email);
ps.setInt(4, seat);
ps.setLong(5, phone);
int x = ps.executeUpdate();
if (x == 1) {
req.getRequestDispatcher("OfferRide1.html").include(req, resp);
}
} catch (Exception e) {
status = e.toString();
//req.getRequestDispatcher("OfferRide1.html").include(req, resp);
}
PrintWriter out = resp.getWriter();
out.print(status);
}
}
place this code in between try and catch block 将此代码放在try和catch块之间
try
{
DateFormat formatter = new SimpleDateFormat("YYYY-MM-DD");
Date date = (Date)formatter.parse(pswd);
}
catch(ParseException e)
{
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.