使用jsp在mysql数据库中插入引导日期选择器日期

r3i60tvu  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(441)

我试图通过jsp在mysql数据库中插入一个日期。使用引导日期选择器接受日期。mysql中shipping\u date和delivery\u date的数据类型是date(也尝试使用datetime)。它给了我无法解释的日期错误。请帮我解决这个问题。提前谢谢`
这里的全部代码是:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ page import="java.sql.*" %>
    <%@page import="java.io.*" %>
    <%@page import="java.text.SimpleDateFormat,java.text.DateFormat,java.text.ParseException" %>
    <%@page import="java.util.Date" %>
    <%@page import="java.lang.*" language="java"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Add PO Details</title>
<h1>Add New Purchase Order Details</h1>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.3.0/css/datepicker3.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.3.0/js/bootstrap-datepicker.js"></script>
</head>
<script>
$(function() {
    var date = new Date();
    date.setDate(date.getDate());
   $('#shipping_date').datepicker({
    autoclose: true,
    todayHighlight: true,
    format: 'yyyy-MM-dd',
     startDate: date
   });
   $('#shipping_date').datepicker().on('changeDate', function() {
    var temp = $(this).datepicker('getDate');
        var d = new Date(temp);
        d.setDate(d.getDate());

        $('#delivery_date').datepicker({
    autoclose: true,
    format: 'yyyy-MM-dd',
    startDate: d,
   });
       $('#delivery_date').datepicker().on('changeDate', function() {
    var temp1 = $(this).datepicker('getDate');
        var d1 = new Date(temp1);
      d1.setDate(d1.getDate());
   });
   });

    });
    </script>
<body>
<form method="post" name="Form" action="" >
<%
String connectionurl="jdbc:mysql://localhost:3306/mydb";
Connection connection=null;
Class.forName("com.mysql.jdbc.Driver");
connection=DriverManager.getConnection(connectionurl,"root","root1216");
ResultSet rs1=null;

    Statement s10=connection.createStatement();
    rs1=s10.executeQuery("select max(pono) from podetails");
    rs1.next();
    int id1=rs1.getInt(1);
    id1=id1+1;
%>
Enter PO number:<input type=text name=pono value="<%=id1%>" readonly><br><br>
Enter shipping date:<input type=text name=shipping_date id="shipping_date" class="form-control datepicker" required><br><br>
Product is shipped via:<input type=text name=shipped_via required><br><br>
Enter delivery date:<input type=text name=delivery_date id="delivery_date" class="form-control datepicker" required><br><br>

<center><input type="submit" name="ADD"></center>
<%
try
{
ResultSet rs=null;
PreparedStatement ps=null;

    if(connection!=null)
        System.out.println("Successfully connected to database\n");
    else
        System.out.println("Failed to connect!!!\n");

String pono1=request.getParameter("pono");
String sdate2=request.getParameter("shipping_date");
String shipped_via=request.getParameter("shipped_via");
String ddate2=request.getParameter("delivery_date");

/* SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    java.util.Date sdate1=sdf.parse(request.getParameter("shipping_date").toString());
    java.sql.Date sdate = new java.sql.Date(sdate1.getTime() ); 
    System.out.println("after shipping");

     SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
     java.util.Date ddate1 =sdf1.parse(request.getParameter("delivery_date").toString());
     java.sql.Date ddate = new java.sql.Date(ddate1.getTime() );     
        System.out.println("after delivery");*/

int pono=Integer.parseInt(pono1.trim());
System.out.println("before insert");
ps=connection.prepareStatement("insert into podetails(pono,shipping_date,shipped_via,delivery_date)values(?,?,?,?)");

    ps.setInt(1,pono);
    //ps.setDate(2,sdate);
    ps.setString(2,sdate2);
    ps.setString(3,shipped_via);
    ps.setString(4,ddate2);
    //ps.setDate(4,ddate);

    int i=ps.executeUpdate();

    System.out.println("Data is Successfully added\n");

    Statement s=connection.createStatement();

    rs=s.executeQuery("select * from podetails");

    while(rs.next())
    {    %>
            <tr>
            <td><%=rs.getInt("pono") %></td>
            <td><%=rs.getDate("shipping_date") %></td>
            <td><%=rs.getString("shipped_via") %></td>
            <td><%=rs.getDate("delivery_date") %></td>
            </tr>
    <br>
    <% 
    System.out.println();}

    connection.close();
    rs.close();
    ps.close();
}
catch(Exception e)
{
    System.out.println("Unable to connect\n");
    e.printStackTrace(System.out);
}
%>
</form>
</body>
</html>
bogh5gae

bogh5gae1#

我通过使用javascript验证而不是bootstrap日期选择器解决了这个问题。下面是代码。。

<script type="text/javascript">
$(function(){
    var dtToday = new Date();

    var month = dtToday.getMonth() + 1;
    var day = dtToday.getDate();
    var year = dtToday.getFullYear();
    if(month < 10)
        month = '0' + month.toString();
    if(day < 10)
        day = '0' + day.toString();

    var maxDate = year + '-' + month + '-' + day;
    $('#shipping_date').attr('min', maxDate);
});

$("input[name='shipping_date']").change(function() {
      $("input[name='delivery_date']").attr('min',$(this).val());
    });

</script>

另外,我将shipping\u date和delivery\u date的输入类型更改为

<input type="date" name="shipping_date" id="shipping_date" required>

相关问题