出生日期将空值存储在数据库

时间:2018-04-30 07:26:42

标签: java mysql jsp servlets

我正在尝试insert数据库中的出生数据。但它只会在null中存储database值。

的index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    <title>Country State List</title>
        <script src="countries.js"></script>
    <title>My JSP 'index.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>

  <body>
    <form action="RegisterServlet" method="post">
   First Name:<input type="text" name="fname"></br>
   Last  Name:<input type="text" name="lname"></br>

         SSN :<input type="text" name="ssn"></br>
      Country:<select id="country" name ="country"></select>
            <br />
        State: <select name ="state" id ="state"></select>

            <br/><br />

            <script language="javascript">
                populateCountries("country", "state");

            </script>
        Date of birth: <input type="date"  name= "dob"/>    
 <input type="submit" value ="register">
    </form>


    </form>
  </body>
</html>

RegisterServleet.java

    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */

    import java.io.IOException;
    import java.io.PrintWriter;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    /**
     *
     * @author srini
     */
    @WebServlet(urlPatterns = {"/RegisterServlet"})
    public class RegisterServlet extends HttpServlet {

        /**
         * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
         * methods.
         *
         * @param request servlet request
         * @param response servlet response
         * @throws ServletException if a servlet-specific error occurs
         * @throws IOException if an I/O error occurs
         */

        public void doGet(HttpServletRequest request,HttpServletResponse response) 
        throws IOException,ServletException{
            processRequset (request,response);
        }

        public void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {

            processRequset (request,response);
        }

        public void processRequset(HttpServletRequest request,HttpServletResponse response) 
                throws IOException,ServletException{
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter out = response.getWriter();
            String fname =request.getParameter("fname");
            String lname =request.getParameter("lname");

            String ssn =request.getParameter("ssn");
            String country =request.getParameter("country");
            String state =request.getParameter("state");
            String dob =request.getParameter("dob");



           try{  
    Class.forName("com.mysql.jdbc.Driver");  
    Connection con=DriverManager.getConnection(  
    "jdbc:mysql://localhost:3306/headway","root","toor");  


        PreparedStatement pstmt=con.prepareStatement("select fname,lname from employee where fname=? " );
        pstmt.setString(1,fname);

        ResultSet rs = pstmt.executeQuery(); 

        if(rs.next())
        {

             out.println("<script type=\"text/javascript\">");
               out.println("alert('User details are already exist ');");
               out.println("location='index.jsp';");
               out.println("</script>");

               /*<select>
               <option value="Java">Java</option>
               <option value="C">C</option>
               <option value="Pascal">Pascal</option>
               <option value="Hadoop">Hadoop</option>
             </select>

             <input type = "text" id = "langText" value = "user selected value">*/
        }
        else {
            PreparedStatement pstmt1=con.prepareStatement("insert into employee(fname,lname,ssn,country,state,dob) values(?,?,?,?,?,?)"); 
              pstmt1.setString(1,fname);
                 pstmt1.setString(2,lname);

                  pstmt1.setString(3,ssn);

                  pstmt1.setString(4,country);
                  pstmt1.setString(5,state);
                  pstmt1.setString(6,dob);

                 int i=pstmt1.executeUpdate();  
                 if(i>0) 
                     out.println("<script type=\"text/javascript\">");
                   out.println("alert('your are successfully Register ');");
                   out.println("location='Display1';");
                   out.println("</script>");

        }
    pstmt.close();
    con.close();

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

    out.close();  
    }  
    }

我的数据库表员工

+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| fname   | varchar(20) | YES  |     | NULL    |       |
| lname   | varchar(20) | YES  |     | NULL    |       |
| ssn     | int(20)     | YES  |     | NULL    |       |
| country | varchar(20) | YES  |     | NULL    |       |
| state   | varchar(20) | YES  |     | NULL    |       |
| dob     | varchar(50) | YES  |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+

table:
+--------+-------+--------+-----------+------------------+------------+
| fname  | lname | ssn    | country   | state            | dob        |
+--------+-------+--------+-----------+------------------+------------+
| srinu  | sk    | 741258 | India     | Telangana        | NULL       |
| naseer | sk    |   5555 | India     | Telangana        | NULL       |
| vamshi | sh    |   4444 | Indonesia | Bengkulu         | NULL       |
| vishal | kl    |   1111 | India     | Telangana        | NULL       |
| manasa | kx    |   7777 | Angola    | Bengo            | NULL       |
| pavani | pa    |      0 | Antartica | Antartica        | NULL       |
| ravi   | ra    |   1234 | Angola    | Andorra la Vella | NULL       |
| NULL   | NULL  |   NULL | NULL      | NULL             | 18-11-1995 |
| NULL   | NULL  |   NULL | NULL      | NULL             | NULL       |
| NULL   | NULL  |   NULL | NULL      | NULL             | NULL       |
+--------+-------+--------+-----------+------------------+------------+

0 个答案:

没有答案