期中考试--人口普查
第七次全国人口普查登记
1、项目需求:
开展第七次全国人口普查,将为编制“十四五”规划提供重要信息支持;推动实现人口与经济社会、资源环境协调发展,为深化供给侧结构性改革,推动经济高质量发展,建设现代化经济体系提供强有力的支持;人口普查还将为完善人口发展战略和政策体系,促进人口长期均衡发展提供重要信息支持。
2.系统要求与功能设计
2.1 页面功能要求
(1)能够在Tomcat服务器中正确部署,并通过浏览器查看;(1分)
(2)网站页面整体风格统一;
图1 人口普查系统功能结构图
(3)主页面:要求显示人口登记、修改人口信息、删除人口信息、查询人口信息,人口信息浏览五个子菜单。(1分)
(4)人口登记页面:(6分)
①完成添加人口信息登记,基本信息和填报限制如下表所示
户 别 |
家庭户 集体户(单选框) |
住房类型 |
家庭住宅、集体住所、工作地住所、其他住宅、无住宅(单选框) |
本户现住房面积 |
XXXXX平方米(文本框,只能输入这个整数) |
本户现住房间数 |
XXXXX间(文本框,只能输入这个整数) |
户主姓名 |
XXXXX(文本框) |
身份证号码 |
XXXXXXXXXXXXXXXXXX(判断身份证位数13位并且只能录入数字、最后一位可以录入X) |
性别 |
男/女(单选框) |
民族 |
(文本框) |
受教育程度 |
研究生、大学本科、大学专科、高中、初中、小学、未上过学(下拉框实现) |
②点击“提交”按钮,保存成功则跳转到人口基本信息浏览界面,新录入的信息置顶显示。失败则提示错误信息,返回当前页面
评分标准:
①完成人口登记页面(未完成0分,完成1分)
②保存人口信息入库(未完成0分,完成1分)
③户别、住房类别、性别实现一个单选框录入0.5分,全部实现1分。(未完成0分,完成一个0.5,全部完成1分)
④本户现住房面积或本户现住房间数实现整数判断(未完成0分,完成0.5分)
⑤判断身份证按照要求录入,实现位数和数字录入判断0.5分,实现最后一位可以输入数字或X判断0.5分。(1分)
⑥受教育程度下拉框录入(未完成0分,完成0.5分)
⑦提交后页面跳转功能;(未完成0分,完成1分)
(5)修改人口信息页面:(3分)
输入户主姓名,显示其余信息,可对身份证号码、性别、民族、受教育程度进行修改。(身份证号码、性别、受教育程度必须符合录入要求);如果该名户主信息数据库不存在,则提示“该户主信息不存在”。(3分)
评分标准:
①完成修改户主个人信息页面(未完成0分,完成0.5分)
② 实现数据库中信息更新(未完成0分,完成1分)
③修改信息判断是否符合要求。(未完成0分,完成0.5分)
④输入姓名,显示其余信息,若该信息不存在,提示错误信息;(未完成0分,完成1分)
(6)删除人口信息页面:录入户主姓名,显示详细信息后,点击“删除”按钮,弹出提示框,提示“是否确认删除该户主信息”,确认后删除该信息。(1分)
评分标准:
①输入户主姓名可显示其余信息。(未完成0分,完成0.5分)
②对应删除数据库中信息(未完成0分,完成0.5分)
(7)浏览人口信息页面:(2分)
以列表形式显示人口基本信息,结果列表中显示姓名、性别,民族、受教育程度基本信息,点击姓名,可以跳转到个人详细信息。
①实现以列表形式显示人口基本信息,结果列表中显示姓名、性别,民族、受教育程度基本信息(未完成0分,完成1分)
②实现跳转个人详细信息页面。(未完成0分,完成1分)
(8)查询人口信息页面:(3分)
要求可以根据人口姓名、性别、民族、受教育程度四种条件实现模糊查询,输出结果以列表形式显示,显示人口姓名、性别、民族、受教育程度基本信息,点击列表中的姓名,跳转到人口个人详细信息页面。
评分标准:
①缺少一种查询条件扣除1分。(未完成0分,完成3分)
2.2 功能要求
(1)设计出合理的数据库和数据表,要求使用mysql、sqlserver、oracle三种数据库中一种(1分)
(2)使用Serverlet实现页面交互(1分)。
(3)使用Java Bean封装数据库连接操作(1分。)
代码如下
import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class servlet */ @WebServlet("/servlet") public class servlet extends HttpServlet { Dao dao = new Dao(); private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public servlet() { super(); // TODO Auto-generated constructor stub } private void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("utf-8"); int id = Integer.parseInt(request.getParameter("id")); String hubie = request.getParameter("hubie"); String livetype = request.getParameter("livetype"); int area = Integer.parseInt(request.getParameter("area")); int roomnum = Integer.parseInt(request.getParameter("roomnum")); String name = request.getParameter("name"); String idcard = request.getParameter("idcard"); String sex = request.getParameter("sex"); String nation = request.getParameter("nation"); String education = request.getParameter("education"); Bean bean = new Bean(id, hubie, livetype, area, roomnum, name, idcard, sex, nation, education); dao.update(bean); request.setAttribute("message", "修改成功"); request.getRequestDispatcher("servlet?method=list").forward(request, response); } private void list(HttpServletRequest request, HttpServletResponse response) throws Exception { // TODO Auto-generated method stub request.setCharacterEncoding("utf-8"); List<Bean> list = dao.list(); request.setAttribute("list", list); request.getRequestDispatcher("list.jsp").forward(request, response); } private void delete(HttpServletRequest request, HttpServletResponse response) throws Exception, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("UTF-8"); int id = Integer.parseInt(request.getParameter("id")); dao.delete(id); //进行数据库的删除操作 request.setAttribute("message", "删除成功"); request.getRequestDispatcher("servlet?method=list").forward(request, response); } private void insert(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { // TODO Auto-generated method stub request.setCharacterEncoding("utf-8"); int id = Integer.parseInt(request.getParameter("id")); String hubie = request.getParameter("hubie"); String livetype = request.getParameter("livetype"); int area = Integer.parseInt(request.getParameter("area")); int roomnum = Integer.parseInt(request.getParameter("roomnum")); String name = request.getParameter("name"); String idcard = request.getParameter("idcard"); String sex = request.getParameter("sex"); String nation = request.getParameter("nation"); String education = request.getParameter("education"); Bean bean = new Bean(id, hubie, livetype, area, roomnum, name, idcard, sex, nation, education); if (dao.insert(bean)) { request.setAttribute("message", "添加成功"); request.getRequestDispatcher("index.jsp").forward(request, response); } } private void search(HttpServletRequest request, HttpServletResponse response) throws Exception, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("UTF-8"); String cxfs = request.getParameter("cxfs"); System.out.print("cxfs"); request.setAttribute("search", "查询成功"); request.getRequestDispatcher("list.jsp").forward(request, response); } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("utf-8"); String method = request.getParameter("method"); if ("insert".equals(method)) { insert(request, response); } else if ("delete".equals(method)) { try { delete(request, response); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } else if ("update".equals(method)) { update(request, response); } else if ("list".equals(method)) { try { list(request, response); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } else if ("search".equals(method)) { try { search(request, response); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } } import java.io.IOException; import java.sql.SQLException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class searchServlet */ @WebServlet("/searchServlet") public class searchServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public searchServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub //response.getWriter().append("Served at: ").append(request.getContextPath()); request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); String cxfs = request.getParameter("cxfs"); System.out.print(cxfs); String str = request.getParameter("value"); Dao dao = new Dao(); List<Bean> list = null; try { if ("1".equals(cxfs)) { list = dao.searchByName(str); } if ("2".equals(cxfs)) { list = dao.searchBySex(str); } if ("3".equals(cxfs)) { list = dao.searchByEducation(str); } if ("4".equals(cxfs)) { list = dao.searchByNation(str); } } catch (SQLException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } request.setAttribute("list", list); request.getRequestDispatcher("list.jsp").forward(request, response); System.out.print(list.size()); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
1 import java.sql.Connection; 2 import java.sql.PreparedStatement; 3 import java.sql.ResultSet; 4 import java.sql.SQLException; 5 import java.sql.Statement; 6 import java.util.ArrayList; 7 import java.util.List; 8 9 public class Dao {//dao层 10 private DBUtil dbutil = new DBUtil(); 11 12 13 public Dao() { 14 // TODO Auto-generated constructor stub 15 } 16 17 public boolean insert(Bean bean) {//插入数据的方法 18 boolean f = false; 19 String sql = "insert into person(id,hubie,livetype,area,roomnum, name, idcard, sex,nation, education) values('" + bean.getId() + "','" + bean.getHubie() + "','" + bean.getLivetype() + "','" + bean.getArea() + "','" + bean.getRoomnum() + "','" + bean.getName() + "','" + bean.getIdcard() + "','" + bean.getSex() + "','" + bean.getNation() + "','" + bean.getEducation() + "')"; 20 Connection conn = DBUtil.getConnection();//数据库连接,加载驱动 21 Statement state = null; 22 try { 23 state = conn.createStatement();//实例化Statement对象,方便对sql语句进行操作 24 System.out.println(conn); 25 state.executeUpdate(sql); 26 f = true; 27 //执行数据库更新操作用于执行INSERT、UPDATE或DELETE语句以及SQLDDL(数据定义语言)语句, 28 //例如CREATETABLE和DROPTABLE,(创建表和删除表) 29 } catch (Exception e)//当try语句中s出现异常时,会执行catch中的语句 30 { 31 e.printStackTrace();//捕获异常的语句 32 } finally //finally作为异常处理的一部分,它只能用在try/catch语句中,并且附带一个语句块,表示这段语句最终一定会被执行(不管有没有抛出异常),经常被用在需要释放资源的情况下。 33 { 34 DBUtil.close(conn); 35 } 36 return f; 37 } 38 39 public boolean delete(int id) {//删除方法 40 String sql = "delete from person where id='" + id + "'"; 41 boolean f = false; 42 Connection conn = DBUtil.getConnection(); 43 Statement st = null; 44 try { 45 st = conn.createStatement(); 46 st.executeUpdate(sql); 47 f = true; 48 } catch (SQLException e) { 49 // TODO Auto-generated catch block 50 e.printStackTrace(); 51 } finally { 52 DBUtil.close(st, conn); 53 } 54 return f; 55 } 56 57 public boolean update(Bean bean) {//更新方法 58 String sql = "update person set hubie='" + bean.getHubie() + "',livetype='" + bean.getLivetype() + "',area='" + bean.getArea() + "',roomnum='" + bean.getRoomnum() + "',name='" + bean.getName() + "',idcard='" + bean.getIdcard() + "',sex='" + bean.getSex() + "',nation='" + bean.getNation() + "',education='" + bean.getEducation() + "'where id='" + bean.getId() + "'"; 59 Connection conn = DBUtil.getConnection(); 60 boolean f = false; 61 Statement st = null; 62 try { 63 st = conn.createStatement(); 64 st.executeUpdate(sql); 65 f = true; 66 } catch (SQLException e) { 67 // TODO Auto-generated catch block 68 e.printStackTrace(); 69 } 70 return f; 71 } 72 73 public List<Bean> list() {//查询所有方法 74 String sql = "select * from person order by id ASC"; 75 Connection conn = DBUtil.getConnection(); 76 Statement st = null; 77 List<Bean> list = new ArrayList<>(); 78 ResultSet rs = null; 79 Bean bean = null; 80 try { 81 st = conn.createStatement(); 82 st.executeQuery(sql); 83 rs = st.executeQuery(sql); 84 while (rs.next()) { 85 int id = rs.getInt("id"); 86 String hubie = rs.getString("hubie"); 87 String livetype = rs.getString("livetype"); 88 int area = rs.getInt("area"); 89 int roomnum = rs.getInt("roomnum"); 90 String name = rs.getString("name"); 91 String idcard = rs.getString("idcard"); 92 String sex = rs.getString("sex"); 93 String nation = rs.getString("nation"); 94 String education = rs.getString("education"); 95 bean = new Bean(id, hubie, livetype, area, roomnum, name, idcard, sex, nation, education); 96 list.add(bean); 97 } 98 } catch (SQLException e) { 99 // TODO Auto-generated catch block 100 e.printStackTrace(); 101 } finally { 102 DBUtil.close(rs, st, conn); 103 } 104 return list; 105 } 106 107 108 public List<Bean> searchByName(String str) throws SQLException {//查询条件方法 109 String sql = "select * from person where(name like '%" + str + "%')"; 110 Connection conn = DBUtil.getConnection(); 111 Statement st = null; 112 PreparedStatement pt = conn.prepareStatement(sql); 113 List<Bean> search = new ArrayList<>(); 114 ResultSet rs = null; 115 Bean bean = null; 116 try { 117 pt = conn.prepareStatement(sql); 118 rs = pt.executeQuery(); 119 while (rs.next()) { 120 int id = rs.getInt("id"); 121 String hubie = rs.getString("hubie"); 122 String livetype = rs.getString("livetype"); 123 int area = rs.getInt("area"); 124 int roomnum = rs.getInt("roomnum"); 125 String name = rs.getString("name"); 126 String idcard = rs.getString("idcard"); 127 String sex = rs.getString("sex"); 128 String nation = rs.getString("nation"); 129 String education = rs.getString("education"); 130 bean = new Bean(id, hubie, livetype, area, roomnum, name, idcard, sex, nation, education); 131 search.add(bean); 132 } 133 } catch (SQLException e) { 134 // TODO Auto-generated catch block 135 e.printStackTrace(); 136 } finally { 137 DBUtil.close(rs, st, conn); 138 } 139 return search; 140 } 141 142 public List<Bean> searchBySex(String str) throws SQLException {//查询条件方法 143 String sql = "select * from person where(sex like '%" + str + "%')"; 144 Connection conn = DBUtil.getConnection(); 145 Statement st = null; 146 PreparedStatement pt = conn.prepareStatement(sql); 147 List<Bean> search = new ArrayList<>(); 148 ResultSet rs = null; 149 Bean bean = null; 150 try { 151 pt = conn.prepareStatement(sql); 152 rs = pt.executeQuery(); 153 while (rs.next()) { 154 int id = rs.getInt("id"); 155 String hubie = rs.getString("hubie"); 156 String livetype = rs.getString("livetype"); 157 int area = rs.getInt("area"); 158 int roomnum = rs.getInt("roomnum"); 159 String name = rs.getString("name"); 160 String idcard = rs.getString("idcard"); 161 String sex = rs.getString("sex"); 162 String nation = rs.getString("nation"); 163 String education = rs.getString("education"); 164 bean = new Bean(id, hubie, livetype, area, roomnum, name, idcard, sex, nation, education); 165 search.add(bean); 166 } 167 } catch (SQLException e) { 168 // TODO Auto-generated catch block 169 e.printStackTrace(); 170 } finally { 171 DBUtil.close(rs, st, conn); 172 } 173 return search; 174 } 175 176 public List<Bean> searchByEducation(String str) throws SQLException {//查询条件方法 177 String sql = "select * from person where(education like '%" + str + "%')"; 178 Connection conn = DBUtil.getConnection(); 179 Statement st = null; 180 PreparedStatement pt = conn.prepareStatement(sql); 181 List<Bean> search = new ArrayList<>(); 182 ResultSet rs = null; 183 Bean bean = null; 184 try { 185 pt = conn.prepareStatement(sql); 186 rs = pt.executeQuery(); 187 while (rs.next()) { 188 int id = rs.getInt("id"); 189 String hubie = rs.getString("hubie"); 190 String livetype = rs.getString("livetype"); 191 int area = rs.getInt("area"); 192 int roomnum = rs.getInt("roomnum"); 193 String name = rs.getString("name"); 194 String idcard = rs.getString("idcard"); 195 String sex = rs.getString("sex"); 196 String nation = rs.getString("nation"); 197 String education = rs.getString("education"); 198 bean = new Bean(id, hubie, livetype, area, roomnum, name, idcard, sex, nation, education); 199 search.add(bean); 200 } 201 } catch (SQLException e) { 202 // TODO Auto-generated catch block 203 e.printStackTrace(); 204 } finally { 205 DBUtil.close(rs, st, conn); 206 } 207 return search; 208 } 209 210 public List<Bean> searchByNation(String str) throws SQLException {//查询条件方法 211 String sql = "select * from person where(nation like '%" + str + "%')"; 212 Connection conn = DBUtil.getConnection(); 213 Statement st = null; 214 PreparedStatement pt = conn.prepareStatement(sql); 215 List<Bean> search = new ArrayList<>(); 216 ResultSet rs = null; 217 Bean bean = null; 218 try { 219 pt = conn.prepareStatement(sql); 220 rs = pt.executeQuery(); 221 while (rs.next()) { 222 int id = rs.getInt("id"); 223 String hubie = rs.getString("hubie"); 224 String livetype = rs.getString("livetype"); 225 int area = rs.getInt("area"); 226 int roomnum = rs.getInt("roomnum"); 227 String name = rs.getString("name"); 228 String idcard = rs.getString("idcard"); 229 String sex = rs.getString("sex"); 230 String nation = rs.getString("nation"); 231 String education = rs.getString("education"); 232 bean = new Bean(id, hubie, livetype, area, roomnum, name, idcard, sex, nation, education); 233 search.add(bean); 234 } 235 } catch (SQLException e) { 236 // TODO Auto-generated catch block 237 e.printStackTrace(); 238 } finally { 239 DBUtil.close(rs, st, conn); 240 } 241 return search; 242 } 243 244 245 } 246 247 248 249 250 251 252 import static org.junit.Assert.*; 253 254 import java.sql.SQLException; 255 256 import org.junit.Test; 257 258 public class DaoTest { 259 260 @Test 261 public void test() throws SQLException { 262 Dao dao=new Dao(); 263 System.out.print(dao.searchByName("2").size()); 264 } 265 }
1 public class Bean { 2 private int id; 3 private String hubie; 4 private String livetype; 5 private int area; 6 private int roomnum; 7 private String name; 8 private String idcard; 9 private String sex; 10 private String nation; 11 private String education; 12 13 public int getId() { 14 return id; 15 } 16 17 public void setId(int id) { 18 this.id = id; 19 } 20 21 public String getHubie() { 22 return hubie; 23 } 24 25 public void setHubie(String hubie) { 26 this.hubie = hubie; 27 } 28 29 public String getLivetype() { 30 return livetype; 31 } 32 33 public void setLivetype(String livetype) { 34 this.livetype = livetype; 35 } 36 37 public int getArea() { 38 return area; 39 } 40 41 public void setArea(int area) { 42 this.area = area; 43 } 44 45 public int getRoomnum() { 46 return roomnum; 47 } 48 49 public void setRoomnum(int roomnum) { 50 this.roomnum = roomnum; 51 } 52 53 public String getName() { 54 return name; 55 } 56 57 public void setName(String name) { 58 this.name = name; 59 } 60 61 public String getIdcard() { 62 return idcard; 63 } 64 65 public void setIdcard(String idcard) { 66 this.idcard = idcard; 67 } 68 69 public String getSex() { 70 return sex; 71 } 72 73 public void setSex(String sex) { 74 this.sex = sex; 75 } 76 77 public String getNation() { 78 return nation; 79 } 80 81 public void setNation(String nation) { 82 this.nation = nation; 83 } 84 85 public String getEducation() { 86 return education; 87 } 88 89 public void setEducation(String education) { 90 this.education = education; 91 } 92 93 public Bean(int id, String hubie, String livetype, int area, int roomnum, String name, String idcard, String sex, String nation, String education) { 94 this.id = id; 95 this.hubie = hubie; 96 this.livetype = livetype; 97 this.area = area; 98 this.roomnum = roomnum; 99 this.name = name; 100 this.idcard = idcard; 101 this.sex = sex; 102 this.nation = nation; 103 this.education = education; 104 } 105 106 public String toString() { 107 return "Census{" + 108 "id=" + id + 109 ", hubie='" + hubie + '\'' + 110 ", livetype='" + livetype + '\'' + 111 ", area=" + area + 112 ", roomnum=" + roomnum + 113 ", name='" + name + '\'' + 114 ", idcard='" + idcard + '\'' + 115 ", sex='" + sex + '\'' + 116 ", nation='" + nation + '\'' + 117 ", education='" + education + '\'' + 118 '}'; 119 } 120 }