简体   繁体   English

如何使用Java将HTML表单中的日期存储到数据库中?

[英]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.

相关问题 如何在bean类属性(java.util.Date)中使用datepicker从html表单存储日期 - how to store date from html form using datepicker in bean class property(java.util.Date) 如何使用Java从数据库中检索日期 - How to retrieve date from database using java 如何使用Java在SQL数据库中存储HTML文本 - How to store HTML text in sql database using Java 如何存储/检索列表 <string> 使用Java从/到数据库的值 - How to store/retrieve List<string> values from/to database using java 如何将日期选择器中的日期存储到mysql数据库中? - How to store date from datepicker into mysql database? 如何从HTML表单获取输入类型“日期”值到Java变量和SQL日期格式? - How can i get input type “date” value from html form into java variable and SQL date format? 如何使用Java从Oracle数据库获取毫秒级的日期/时间? - How to get date/time with millisecond from oracle database using java? 如何使用java swing文本框输入日期并存储在mysql数据库中? - how to take an input of date using java swing text box and store in mysql database? 如何使用Java将视频下载/存储到数据库中? - How to download/store videos into database using java? 如何使用Java提取字符串并将其存储到数据库 - how to extract string and store to database using java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM