java-web 第七次人口普查征集系统开发日记三

时间:2024-02-25 15:33:30

人口登记功能实现

 

界面:

 

 

界面源代码:

  1 <%@ page language="java" contentType="text/html; charset=utf-8"
  2     pageEncoding="utf-8"%>
  3 <!DOCTYPE html>
  4 <html>
  5 <head>
  6 <meta charset="utf-8">
  7 <title>第七次全国人口普查-人口信息登记</title>
  8 
  9 <link rel="stylesheet" href="layui/css/layui.css" media="all">
 10 <script src="layui/layui.js" type="text/javascript"></script>
 11 <script src="jquery/jquery-1.9.1.min.js" type="text/javascript"></script>
 12 
 13 
 14 
 15 </head>
 16 <body>
 17 <form class="layui-form" action="PeopleAddServlet" method="post">
 18 <br>
 19 <br>
 20 <h1 align="center">第七次全国人口普查-人口信息登记</h1>
 21 <br>
 22 <br>
 23 <!-- 户别选择-->
 24  <div class="layui-form-item">
 25     <label class="layui-form-label">户 别:</label>
 26     <div class="layui-input-block">
 27       <input type="radio" name="HB" value="家庭户" title="家庭户">
 28       <input type="radio" name="HB" value="集体户" title="集体户">
 29     </div>
 30   </div>
 31   
 32   <!-- 住房类型 -->
 33   <div class="layui-form-item">
 34     <label class="layui-form-label">住房类型:</label>
 35     <div class="layui-input-block">
 36       <input type="radio" name="ZFLX" value="家庭住宅" title="家庭住宅">
 37       <input type="radio" name="ZFLX" value="集体住所" title="集体住所">
 38        <input type="radio" name="ZFLX" value="工作地住所" title="工作地住所">
 39         <input type="radio" name="ZFLX" value="其他住宅" title="其他住宅">
 40          <input type="radio" name="ZFLX" value="无住宅" title="无住宅">
 41     </div>
 42   </div>
 43   
 44   <!-- 现住房面积 -->
 45   <div class="layui-form-item">
 46     <label class="layui-form-label">现住房面积:</label>
 47     <div class="layui-input-inline">
 48       <input type="text" name="XZFMJ" required  lay-verify="required" placeholder="请输入面积" autocomplete="off" class="layui-input">
 49     </div>
 50   </div>
 51   
 52   <!-- 现住房间数 -->
 53   <div class="layui-form-item">
 54     <label class="layui-form-label">现住房间数:</label>
 55     <div class="layui-input-inline">
 56       <input type="text" name="XZFJS" required lay-verify="required" placeholder="请输入间数" autocomplete="off" class="layui-input">
 57     </div>
 58   </div>
 59   
 60   <!-- 户主姓名 -->
 61   <div class="layui-form-item">
 62     <label class="layui-form-label">户主姓名:</label>
 63     <div class="layui-input-inline">
 64       <input type="text" name="HZXM" required lay-verify="required" placeholder="请输入户主姓名" autocomplete="off" class="layui-input">
 65     </div>
 66   </div>
 67   <!-- 身份证号码 -->
 68   <div class="layui-form-item">
 69     <label class="layui-form-label">身份证号码:</label>
 70     <div class="layui-input-inline">
 71       <input type="text" name="SFZHM" required lay-verify="required" placeholder="请输入身份证号码" autocomplete="off" class="layui-input">
 72     </div>
 73   </div>
 74   
 75   <!-- 性别 -->
 76   <div class="layui-form-item">
 77     <label class="layui-form-label">性别:</label>
 78     <div class="layui-input-block">
 79       <input type="radio" name="XB" value="男" title="男">
 80       <input type="radio" name="XB" value="女" title="女">
 81     </div>
 82   </div>
 83   
 84   <!-- 民族 -->
 85   <div class="layui-form-item">
 86     <label class="layui-form-label">民族:</label>
 87     <div class="layui-input-inline">
 88       <input type="text" name="MZ" required lay-verify="required" placeholder="请输入民族" autocomplete="off" class="layui-input">
 89     </div>
 90   </div>
 91   
 92   <!-- 受教育程度 --> 
 93  <div class="layui-form-item">
 94     <label class="layui-form-label">受教育程度:</label>
 95     <div class="layui-input-inline">
 96       <select name="SJYCD" lay-verify="required">
 97        
 98         <option value="研究生">研究生</option>
 99         <option value="大学本科">大学本科</option>
100         <option value="大学专科">大学专科</option>
101         <option value="高中">高中</option>
102         <option value="初中">初中</option>
103         <option value="小学">小学</option>
104         <option value="未上过学">未上过学</option>
105       </select>
106     </div>
107   </div>
108   
109 <!-- 提交 -->
110   <div class="layui-form-item">
111     <div class="layui-input-block">
112       <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
113       <button type="reset" class="layui-btn layui-btn-primary">重置</button>
114     </div>
115   </div>
116 </form>
117  
118 <script>
119 //Demo
120 layui.use(\'form\', function(){
121   var form = layui.form;
122   
123   //监听提交
124   
125 });
126 </script>
127 </body>
128 </html>

 

功能接口实现(实现SQL语句的编写和执行,完成人口信息的存储):

 1 @Override
 2     public boolean add(People people) {
 3         
 4         // TODO Auto-generated method stub
 5         boolean flag=false;
 6         try {
 7             String sql="insert into huseventhpeoplecheck  values(\'"+people.getTIME()+"\',\'"+people.getHB()+"\',\'"+people.getZFLX()+"\',\'"+people.getXZFMJ()+"\',\'"+people.getXZFJS()+"\',\'"+people.getHZXM()+"\',\'"+people.getSFZHM()+"\',\'"+people.getXB()+"\',\'"+people.getMZ()+"\',\'"+people.getSJYCD()+"\')";
 8             PreparedStatement pstmt = conn.prepareStatement(sql);
 9             int i=pstmt.executeUpdate();
10             pstmt.close();
11             conn.close();
12             if(i>0) {
13                 flag=true;
14             }
15         }catch(SQLException e) {
16             System.out.println("人口信息登记失败!!!");
17             e.printStackTrace();
18         }
19         return flag;
20     }

 

 

servlet源代码(主要从界面获取所填写的值,并调用接口中的方法,将其存储入数据库中):

 1 package service;
 2 
 3 import java.io.IOException;
 4 import java.text.SimpleDateFormat;
 5 import java.util.Date;
 6 
 7 import javax.servlet.ServletException;
 8 import javax.servlet.annotation.WebServlet;
 9 import javax.servlet.http.HttpServlet;
10 import javax.servlet.http.HttpServletRequest;
11 import javax.servlet.http.HttpServletResponse;
12 
13 
14 import dao.PeopleDao;
15 import dao.PeopleDaoImpI;
16 import entity.People;
17 
18 /**
19  * Servlet implementation class PeopleAddServlet
20  */
21 @WebServlet("/PeopleAddServlet")
22 public class PeopleAddServlet extends HttpServlet {
23     private static final long serialVersionUID = 1L;
24        
25    
26     /**
27      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
28      */
29     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
30         request.setCharacterEncoding("utf-8");
31         response.setContentType("text/html;charset=utf-8");
32 
33         SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd \'at\' HH:mm:ss z");
34         Date date = new Date(System.currentTimeMillis());
35         
36         
37         String HB = request.getParameter("HB");
38         String ZFLX = request.getParameter("ZFLX");
39         String  XZFMJ = request.getParameter("XZFMJ");
40         String XZFJS = request.getParameter("XZFJS");
41         String HZXM = request.getParameter("HZXM");
42         String SFZHM = request.getParameter("SFZHM");
43         String XB = request.getParameter("XB");
44         String MZ = request.getParameter("MZ");
45         String SJYCD = request.getParameter("SJYCD");
46         String TIME = formatter.format(date);
47         
48         System.out.println(SFZHM);
49         
50         People p = new People();
51         p.setHB(HB);
52         p.setZFLX(ZFLX);
53         p.setXZFMJ(XZFMJ);
54         p.setXZFJS(XZFJS);
55         p.setHZXM(HZXM);
56         p.setSFZHM(SFZHM);
57         p.setXB(XB);
58         p.setMZ(MZ);
59         p.setSJYCD(SJYCD);
60         p.setTIME(TIME);
61         
62         
63         PeopleDao pd = new PeopleDaoImpI();
64         try {
65             pd.add(p);
66             response.sendRedirect("renkoudengji.jsp");
67             System.out.println("人口添加成功!!!");
68         }catch(Exception e){
69             System.out.println("人口信息添加失败!!!");
70             e.printStackTrace();
71         }
72         
73     }
74 
75 }

 

 

 该部分主要是人口信息登记方面的功能实现,其最核心的部分就是接口方法的实现,servlet中调用即可。