How to Insert a date in MYSQL using JSP and Servlet

How to read the date from a date picker using JSP and servlet and Insert the date into the MYSQL database using JSP, Servlet, and date picker.

Date has a specific format like dd/MM/yyyy or MM/dd/yyyy and the type of date field will be a date. There are two ways to insert a date into MySQL. Convert a date into String and insert as string type but it’s not a good approach. let’s see another way.

Take input in date format and store it into date format only. also, We do not need to do String to date and Date to string conversion at every stage.

Insert a date in MySQL database using JSP and Servlet

  • Create a database “mydb” with table name “emp” with column name dob.
  • Create an index.jsp file. design datepicker and from to send a request to the servlet.
  • Create a servlet Datepicker.java to get the request, database connection, and send the response.

Database setup

Create a database “mydb” with table name “emp” with column name dob.

CREATE SCHEMA `mydb` ;
CREATE TABLE `mydb`.`emp` (
`dob` DATETIME NULL);

Create JSP

Create a JSP file “index.jsp” and write the below code to create a simple date picker to select a date. To create a date picker. Here, we are using JQuery. and calling $("#datepicker").datepicker();Where date #datepicker is the ID of the input field.

<%-- 
    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 Servlet

Create a servlet file “DatePicker.java” to handle the user request and response. When the user will select a date from the date picker and submit the form. Datepicker.java servlet will get the input parameter and create a new MYSQL connection and insert date into MYSQL.

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/mydb", "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: If you are using Java dynamic web application. Remember for creating the connection between Java and MYSQL you need a “MySQL-connector” please download or import it into your library or watch the video.

If you are using a maven project in java then add the MYSQL dependency into pom.xml

<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.23</version>
</dependency>

Insert a date in MYSQL using JSP and Servlet (Video Tutorial)

Practise Task in JSP

Viva Questions and answers for java project

Check new projects in java