You are not logged in. Please login at www.codechef.com to post your questions!

×

how to store date from datepicker of jsp via servlet in derby database?

reservation.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"> <title>reservation Page</title>
    <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
    <!--<link rel="stylesheet" href="/resources/demos/style.css">-->
    <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

    <script>
        $(function () {
            $("#datepicker").datepicker();
        });
    </script>

</head>

<body>

  <input type="text" class="form-control" name="datepicker" id="datepicker" placeholder="Date" required=""/>
  <input type="text" class="form-control" name="time"  id="timepicker" placeholder="Time" required=""/>

  <select id="party" name="noOfparty" placeholder="Number of Party" class="form-control" required="">
                    <option>Number of Party</option> 
                    <option>1</option>
                    <option>2</option>
                    <option>3</option>
                    <option>4</option>
                    <option>5</option>
                    <option>6</option>
                    <option>7</option>
                    <option>8</option>
                    <option>9</option>
                    <option>10</option>
                    <option>11</option>
                    <option>12</option>
                    <option>13</option>
 </select>

  <button class="btn btn-lg btn-primary btn-block"  name="Submit" value="reserve" type="Submit"> Reserve 
  </button>

</body>

ReserveServlet.java

package Servlet;

import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.text.ParseException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;

public class ReserveServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

//private static final Date Date = null;
public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();

   // String dateString = request.getParameter("date");
    //Date Date = request.getParameter("picker");

    String date = request.getParameter("datepicker");

    //java.text.DateFormat df = new java.text.SimpleDateFormat("MM/dd/yyyy");
    java.text.DateFormat format = new java.text.SimpleDateFormat("MM/dd/yyyy");
    try {
        //java.util.Date d = df.parse(Date);
        java.util.Date da = format.parse(date);
    } catch (ParseException ex) {
        //Logger.getLogger(ReserveServlet.class.getName()).log(Level.SEVERE, null, ex);
        System.out.println(ex);
    }

    String Time = request.getParameter("time");
    int party = Integer.parseInt(request.getParameter("noOfparty"));

    try {
        Class.forName("org.apache.derby.jdbc.ClientDriver");
        System.out.println("driver loaded");

        Connection con = DriverManager.getConnection("jdbc:derby://localhost:1527/test", "test", "test");
        System.out.println("connection success");

        PreparedStatement ps = con.prepareStatement("insert into reserve values(?,?,?)");
        System.out.println("values inserted");

        //ps.setDate(1, Date);
        java.sql.Date da= new java.sql.Date(da.getTime());
        ps.setDate(1, da);
        ps.setString(2, Time);
        ps.setInt(3, party);

        int i = ps.executeUpdate();
        if (i > 0) {
            out.print("Your Table is successfully reserved...");
        }

    } catch (Exception e2) {
        System.out.println(e2);
    }

    out.close();
}

}

asked 19 Aug '17, 02:22

darshana40's gravatar image

0★darshana40
11
accept rate: 0%

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×220

question asked: 19 Aug '17, 02:22

question was seen: 1,231 times

last updated: 19 Aug '17, 02:22