如何从html表单获取日期到servlet

问题描述 投票:0回答:2

我有一个HTML表单,我有一个日期和时间选择器。我想要做的是在servlet中通过request.getParameter获取日期和时间,然后将其插入到我的数据库中。如何以日期和时间格式获取日期和时间,或者将它们作为字符串然后解析它们。

码:

String[] quantity = request.getParameterValues("Quantity_field");
     java.util.Date selectedDate = new SimpleDateFormat("yyyy-MM-dd").parse(request.getParameter("deliveryDate")); //using this one i am trying to get date but it is showing error
    java.util.Date selectedTime = new SimpleDateFormat("hh:mm:ss").parse(request.getParameter("deliveryTime")); 
    System.out.println(selectedDate);
    java.util.Date dateUtil = new Date();
    java.sql.Date dateSql = new java.sql.Date(dateUtil.getTime());
    java.sql.Timestamp timestamp = new Timestamp(dateUtil.getTime());


    try {
        con = DBConnection.createConnection();
        statement = con.createStatement();
        String query = " insert query  VALUES   (?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        PreparedStatement ps = con.prepareStatement(query);
        for (int i = 0; i < quantity.length; i++) {
            if (Double.parseDouble(quantity[i]) != 0) {
                if(recorderNo!=0)
                {
                    ps.setInt(1, recorderNo);
                }
                else
                {
                    ps.setInt(1, 1);
                }

                ps.setDate(2, dateSql);
                ps.setString(3, 77);
                ps.setDouble(4, 55);
                ps.setInt(5, 0);
                ps.setInt(6, 0);
                ps.setDouble(7, Double.parseDouble(quantity[i]));
                ps.setInt(8, 0);
                ps.setInt(9, 0);
                ps.setInt(10, 0);
                ps.setString(11, outlet);
                ps.setTimestamp(12, timestamp);
                 ps.setDate(13,selectedDate); // here i am trying to put date
                ps.setTime(14,selectedTime);// and here time
                ps.addBatch();
            }
        }
        ps.executeBatch();

我在谷歌上发现了这个以获取日期和时间,但它在以下情况下抛出错误:

ps.setDate(13,selectedDate);
ps.setTime(14,selectedTime);

错误是:

PreparedStatement类型中的方法setDate(int,java.sql.Date)不适用于参数(int,java.util.Date)

编辑

我这样做

java.util.Date selectedDate = new SimpleDateFormat("yyyy-MM-dd").parse(request.getParameter("deliveryDate")); 
    java.util.Date selectedTime = new SimpleDateFormat("hh:mm:ss").parse(request.getParameter("deliveryTime"));
    java.sql.Date dateSelected = new java.sql.Date(selectedDate.getTime());     
    java.sql.Time dateTime=new Time(selectedTime.getTime());

但上面的行是投掷错误,我想我错过了什么或写错了

java servlets
2个回答
0
投票

你的java.util.Date selectedDatejava.util.Date类型,而PreparedStatement的setDate(int, java.sql.Date)方法需要java.sql.Date类型。所以将你的java.util.Date转换为java.sql.Date

您可以添加以下行:

java.util.Date selectedDate = new SimpleDateFormat("yyyy-MM-dd").parse(request.getParameter("deliveryDate")); 
java.util.Date selectedTime = new SimpleDateFormat("hh:mm:ss").parse(request.getParameter("deliveryTime")); 
// Add the lines below
java.sql.Date selectedSQLDate =  new java.sql.Date(selectedDate.getTime());
java.sql.Time selectedSQLTime =  new java.sql.Time(selectedTime.getTime());

所以你的代码将是:

        // Other codes
 try { 
        String[] quantity = request.getParameterValues("Quantity_field");
        java.util.Date selectedDate = new SimpleDateFormat("yyyy-MM-dd").parse(request.getParameter("deliveryDate")); 
        java.util.Date selectedTime = new SimpleDateFormat("hh:mm:ss").parse(request.getParameter("deliveryTime")); 
        // Add the lines below
        java.sql.Date selectedSQLDate =  new java.sql.Date(selectedDate.getTime());
        java.sql.Time selectedSQLTime =  new java.sql.Time(selectedTime.getTime());
        con = DBConnection.createConnection();
        statement = con.createStatement();
        String query = " insert query  VALUES   (?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        PreparedStatement ps = con.prepareStatement(query);
        for (int i = 0; i < quantity.length; i++) {
            if (Double.parseDouble(quantity[i]) != 0) {
                if(recorderNo!=0)
                {
                    ps.setInt(1, recorderNo);
                }
                else
                {
                    ps.setInt(1, 1);
                }

                ps.setDate(2, dateSql);
                ps.setString(3, 77);
                ps.setDouble(4, 55);
                ps.setInt(5, 0);
                ps.setInt(6, 0);
                ps.setDouble(7, Double.parseDouble(quantity[i]));
                ps.setInt(8, 0);
                ps.setInt(9, 0);
                ps.setInt(10, 0);
                ps.setString(11, outlet);
                ps.setTimestamp(12, timestamp);
                ps.setDate(13,selectedDate); // here i am trying to put date
                ps.setTime(14,selectedTime);// and here time
                ps.addBatch();
            }
        }
        ps.executeBatch();
        // Other codes

} catch (Exception e){
       // TODO Auto-generated catch block 
}

但我建议您使用java.time类而不是传统的java.util.Datejava.sql.Date与JDBC 4.2或更高版本。有关详细信息,请参阅this


0
投票

您有两个不同的Date对象(util.Date和sql.Date),因此您需要从util.Date转换为sql.Date以使用setDate方法。

Explanation in this post

© www.soinside.com 2019 - 2024. All rights reserved.