如何使用java将日期从Html表单存储到数据库?

时间:2016-09-11 14:15:21

标签: java html mysql

我已经通过html页面输入了日期并尝试将其存储到数据库但是我得到了这个异常java.lang.NumberFormatException:对于输入字符串:" 09/12/2016"我在netbeans中也收到错误"用Date date = (Date)formatter.parse(pswd);try"围绕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);                            

    }

}

1 个答案:

答案 0 :(得分:0)

将此代码放在try和catch块之间

// Create a reference to the file you want to download
let starsRef = storageRef.child("images/stars.jpg")
// Fetch the download URL
starsRef.downloadURLWithCompletion { (URL, error) -> Void in
  if (error != nil) {
    // Handle any errors
  } else {
    // TODO: download from URL with a REST GET call
  }
}