mysql date || Java web application|| inserts a date in the database using date picker, JSP,MySQL.

How to read the date from a date picker using JSP and servlet and insert the date in the database using JSP and date picker. Jquery make this task easy for me to perform an operation on MySQL date. In this tutorial, we will see how to do that.

Insert a MYSQL date in the database using date picker.

Create a JSP file “index.jsp”

<%-- 
    Document   : index
    Created on : Nov 1, 2017, 4:58:44 PM
    Author     : hp
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <title>Java Date Picker</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.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>
        <Pre>
        <form action="DatePicker">
            <label for="datepicker">Enter Your Date of Birth:</label>
                <input type="text" name="dob" id="datepicker">
            
            <input type="submit" value="Submit">
        </form>
        </pre>    
    </body>
</html>

Create a servlet file “DatePicker.java”

Read date from index.jsp and insert into MYSQL database.

try {
                /* TODO output your page here. You may use following sample code. */
                String dob = request.getParameter("dob");
                
                Class.forName("com.mysql.jdbc.Driver");
                
                Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/demo", "root", "root");
                 
               Statement stmt = con.createStatement();
                
                stmt.executeUpdate("insert into emp(dob) values('"+dob+"')");
                
                out.println("Your Date of birth "+dob+ " is submited is database");
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(DatePicker.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SQLException ex) {
                Logger.getLogger(DatePicker.class.getName()).log(Level.SEVERE, null, ex);
            }

Note: Remember for creating the connection between Java and MYSQL you need a “mysql-connector” please download or import into your library or watch the video.

 

 

One Comment

Add a Comment

Your email address will not be published. Required fields are marked *

eleven − 6 =