2020.11.10
一、今日学习内容
今天进行了期中考试,自己认为做的不是很好
人口普查登记系统:
package Dao; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.List; import entity.People; import util.DBUtil; /** * 课程Dao * Dao层操作数据 * @author Hu * */ public class PeopleDao { /** * 添加 * @param course * @return */ public boolean add(People people) { String sql = "insert into people(hubie,leixing,area,number,name,IDcard,sex,minzu,edu) values('" + people.getHubie() + "','" + people.getLeixing() + "','" + people.getArea() +"','"+ people.getNumber()+"','"+people.getName()+"','"+people.getIDcard()+"','"+people.getSex()+"','"+people.getMinzu()+"','"+ people.getEdu()+ "')"; //创建数据库链接 Connection conn = DBUtil.getConn(); Statement state = null; boolean f = false; int a = 0; try { state = conn.createStatement(); state.executeUpdate(sql); } catch (Exception e) { e.printStackTrace(); } finally { //关闭连接 DBUtil.close(state, conn); } if (a > 0) { f = true; } return f; } /** * 删除 * * @param id * @return */ public boolean delete (String name) { boolean f = false; String sql = "delete from people where name='" + name + "'"; Connection conn = DBUtil.getConn(); Statement state = null; int a = 0; try { state = conn.createStatement(); a = state.executeUpdate(sql); } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(state, conn); } if (a > 0) { f = true; } return f; } /** * 修改 * @param name * @param pass */ public boolean update(People people) { String sql = "update people set hubie='" + people.getHubie() + "', leixing='" + people.getLeixing() + "', area='" + people.getArea()+"', number='" + people.getNumber() +"', name='" + people.getName() +"',IDcard='" + people.getIDcard() +"', sex='" + people.getSex() +"', minzu='" + people.getMinzu()+"', edu='" + people.getEdu()+ "' where id='" + people.getId() + "'"; Connection conn = DBUtil.getConn(); Statement state = null; boolean f = false; int a = 0; try { state = conn.createStatement(); a = state.executeUpdate(sql); } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(state, conn); } if (a > 0) { f = true; } return f; } /** * 验证课程名称是否唯一 * true --- 不唯一 * @param name * @return */ public boolean name(String name) { boolean flag = false; String sql = "select name from people where name = '" + name + "'"; Connection conn = DBUtil.getConn(); Statement state = null; ResultSet rs = null; try { state = conn.createStatement(); rs = state.executeQuery(sql); while (rs.next()) { flag = true; } } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return flag; } /** * 通过ID得到课程信息 * @param id * @return */ public People getPeopleById(int id) { String sql = "select * from people where id ='" + id + "'"; Connection conn = DBUtil.getConn(); Statement state = null; ResultSet rs = null; People people = null; try { state = conn.createStatement(); rs = state.executeQuery(sql); while (rs.next()) { String hubie=rs.getString("hubie"); String leixing=rs.getString("leixing"); String area = rs.getString("area"); String number = rs.getString("number"); String name1= rs.getString("name"); String IDcard =rs.getString("IDcard"); String sex=rs.getString("sex"); String minzu=rs.getString("minzu"); String edu=rs.getString("edu"); people = new People(id,hubie,leixing,area,number,name1,IDcard,sex,minzu,edu); } } catch (Exception e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return people; } /** * 通过name得到People * @param name * @return */ public People getPeopleByname(String name) { String sql = "select * from people where name ='" + name + "'"; Connection conn = DBUtil.getConn(); Statement state = null; ResultSet rs = null; People people = null; try { state = conn.createStatement(); rs = state.executeQuery(sql); while (rs.next()) { String hubie=rs.getString("hubie"); String leixing=rs.getString("leixing"); String area = rs.getString("area"); String number = rs.getString("number"); String name1= rs.getString("name"); String IDcard1 =rs.getString("IDcard"); String sex=rs.getString("sex"); String minzu=rs.getString("minzu"); String edu=rs.getString("edu"); people = new People(hubie,leixing,area,number,name1,IDcard1,sex,minzu,edu); } } catch (Exception e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return people; } /** * 查找 * @param name * @param teacher * @param classroom * @return */ public List<People> search(String hubie,String leixing, String area, String number, String name, String IDcard, String sex,String minzu,String edu) { String sql = "select * from people where "; if (name != "") { sql += "name like '%" + name + "%'"; } if (IDcard != "") { sql += "teacher like '%" + IDcard + "%'"; } if (sex!= "") { sql += "classroom like '%" + sex + "%'"; } List<People> list = new ArrayList<>(); Connection conn = DBUtil.getConn(); Statement state = null; ResultSet rs = null; try { state = conn.createStatement(); rs = state.executeQuery(sql); People bean = null; while (rs.next()) { int id = rs.getInt("id"); String hubie1=rs.getString("hubie"); String leixing1=rs.getString("leixing"); String area1 = rs.getString("area"); String number1 = rs.getString("number"); String name1= rs.getString("name"); String IDcard1 =rs.getString("IDcard"); String sex1=rs.getString("sex"); String minzu1=rs.getString("minzu"); String edu1=rs.getString("edu"); bean = new People(id, hubie1,leixing1,area1,number1,name1,IDcard1,sex1,minzu1,edu1); list.add(bean); } } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return list; } /** * 全部数据 * @param name * @param teacher * @param classroom * @return */ public List<People> list() { String sql = "select * from people"; List<People> list = new ArrayList<>(); Connection conn = DBUtil.getConn(); Statement state = null; ResultSet rs = null; try { state = conn.createStatement(); rs = state.executeQuery(sql); People bean = null; while (rs.next()) { int id1 = rs.getInt("id"); String hubie1=rs.getString("hubie"); String leixing1=rs.getString("leixing"); String area1 = rs.getString("area"); String number1 = rs.getString("number"); String name1 = rs.getString("name"); String IDcard1 =rs.getString("IDcard"); String sex1=rs.getString("sex"); String minzu1=rs.getString("minzu"); String edu1=rs.getString("edu"); bean = new People(id1, hubie1,leixing1,area1,number1,name1,IDcard1,sex1,minzu1,edu1); list.add(bean); } } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return list; } }
package util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; /** * 数据库连接工具 * @author Hu * */ public class DBUtil { public static String db_url = "jdbc:mysql://localhost:3306/test1?serverTimezone=UTC"; public static String db_user = "root"; public static String db_pass = "08301016"; public static Connection getConn() { Connection conn = null; try { Class.forName("com.mysql.cj.jdbc.Driver");//加载驱动 conn = DriverManager.getConnection(db_url, db_user, db_pass); } catch (Exception e) { e.printStackTrace(); } return conn; } /** * 关闭连接 * @param state * @param conn */ public static void close (Statement state, Connection conn) { if (state != null) { try { state.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void close (ResultSet rs, Statement state, Connection conn) { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (state != null) { try { state.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void main(String[] args) throws SQLException { Connection conn = getConn(); PreparedStatement pstmt = null; ResultSet rs = null; String sql ="select * from people"; pstmt = conn.prepareStatement(sql); rs = pstmt.executeQuery(); if(rs.next()){ System.out.println("空"); }else{ System.out.println("不空"); } } }
package entity; public class People { private int id; private String hubie;//单选框 private String leixing; //单选框 private String area; private String number; private String name; private String IDcard; private String sex; //单选框 private String minzu; private String edu; //下拉框 public int getId() { return id; } public void setId(int id) { this.id = id; } public String getHubie() { return hubie; } public void setHubie(String hubie) { this.hubie = hubie; } public String getLeixing() { return leixing; } public void setLeixing(String leixing) { this.leixing = leixing; } public String getArea() { return area; } public void setArea(String area) { this.area = area; } public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getIDcard() { return IDcard; } public void setIDcard(String IDcard) { this.IDcard = IDcard; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getMinzu() { return minzu; } public void setMinzu(String minzu) { this.minzu = minzu; } public String getEdu() { return edu; } public void setEdu(String edu) { this.edu = edu; } public People(int id, String hubie,String leixing, String area, String number, String name, String IDcard, String sex,String minzu,String edu) { super(); this.id = id; this.hubie=hubie; this.leixing=leixing; this.area=area; this.number=number; this.name = name; this.IDcard=IDcard; this.sex = sex; this.minzu = minzu; this.edu=edu; } public People(String hubie,String leixing, String area, String number, String name, String IDcard, String sex,String minzu,String edu) { super(); this.hubie=hubie; this.leixing=leixing; this.area=area; this.number=number; this.name = name; this.IDcard=IDcard; this.sex = sex; this.minzu = minzu; this.edu=edu; } public People() {} }
package service; import java.util.List; import Dao.PeopleDao; import entity.People; /** * CourseService * 服务层 * @author Hu * */ public class PeopleService { PeopleDao pDao = new PeopleDao(); /** * 添加 * @param course * @return */ public boolean add(People people) { boolean f = false; if(!pDao.name(people.getName())) { pDao.add(people); f = true; } return f; } /** * 删除 */ public void del(String name) { pDao.delete(name); } /** * 修改 * @return */ public void update(People people) { pDao.update(people); } /** * 通过ID得到一个Course * @return */ public People getPeopleById(int id) { return pDao.getPeopleById(id); } /** * 通过Name得到一个Course * @return */ public People getPeopleByname(String name) { return pDao.getPeopleByname(name); } /** * 查找 * @return */ public List<People> search(String hubie,String leixing, String area, String number, String name, String IDcard, String sex,String minzu,String edu) { return pDao.search(hubie,leixing,area,number,name,IDcard,sex,minzu,edu); } /** * 全部数据 * @return */ public List<People> list() { return pDao.list(); } }
package servlet; import java.io.IOException; 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; import entity.People; import service.PeopleService; @WebServlet("/PeopleServlet") public class PeopleServlet extends HttpServlet { private static final long serialVersionUID = 1L; PeopleService service = new PeopleService(); /** * 方法选择 */ protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); String method = req.getParameter("method"); if ("add".equals(method)) { add(req, resp); } else if ("del".equals(method)) { del(req, resp); } else if ("update".equals(method)) { update(req, resp); } else if ("search".equals(method)) { search(req, resp); } else if ("getPeopleById".equals(method)) { getPeopleById(req, resp); } else if ("getPeopleByname".equals(method)) { getPeopleByname(req, resp); } else if ("list".equals(method)) { list(req, resp); } } /** * 添加 * @param req * @param resp * @throws IOException * @throws ServletException */ private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { req.setCharacterEncoding("utf-8"); //获取数据 String hubie=req.getParameter("hubie"); String leixing=req.getParameter("leixing"); String area = req.getParameter("area"); String number = req.getParameter("number"); String name = req.getParameter("name"); String IDcard =req.getParameter("IDcard"); String sex=req.getParameter("sex"); String minzu=req.getParameter("minzu"); String edu=req.getParameter("edu"); People people=new People(hubie,leixing,area,number,name,IDcard,sex,minzu,edu); //添加后消息显示 if(service.add(people)) { req.setAttribute("message", "添加成功"); req.getRequestDispatcher("add.jsp").forward(req,resp); } else { req.setAttribute("message", "名称重复,请重新录入"); req.getRequestDispatcher("add.jsp").forward(req,resp); } } /** * 全部 * @param req * @param resp * @throws ServletException */ private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); List<People> peoples = service.list(); //Object peoples = null; req.setAttribute("peoples", peoples); req.getRequestDispatcher("list.jsp").forward(req,resp); } /** * 通过ID得到Course * @param req * @param resp * @throws ServletException */ private void getPeopleById(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); int id = Integer.parseInt(req.getParameter("id")); People people = service.getPeopleById(id); req.setAttribute("people", people); req.getRequestDispatcher("detail2.jsp").forward(req,resp); } /** * 通过名字查找 * 跳转至删除 * @param req * @param resp * @throws IOException * @throws ServletException */ private void getPeopleByname(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); String name = req.getParameter("name"); People people = service.getPeopleByname(name); if(people == null) { req.setAttribute("message", "查无此信息!"); req.getRequestDispatcher("del.jsp").forward(req,resp); } else { req.setAttribute("people", people); req.getRequestDispatcher("detail.jsp").forward(req,resp); } } /** * 删除 * @param req * @param resp * @throws IOException * @throws ServletException */ private void del(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); String name=req.getParameter("name"); service.del(name); req.setAttribute("message", "删除成功!"); req.getRequestDispatcher("PeopleServlet?method=list").forward(req,resp); } /** * 修改 * @param req * @param resp * @throws IOException * @throws ServletException */ private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); int id = Integer.parseInt(req.getParameter("id")); String hubie=req.getParameter("hubie"); String leixing=req.getParameter("leixing"); String area = req.getParameter("area"); String number = req.getParameter("number"); String name = req.getParameter("name"); String IDcard =req.getParameter("IDcard"); String sex=req.getParameter("sex"); String minzu=req.getParameter("minzu"); String edu=req.getParameter("edu"); People people = new People(id,hubie,leixing,area,number,name,IDcard,sex,minzu,edu); service.update(people); req.setAttribute("message", "修改成功"); req.getRequestDispatcher("PeopleServlet?method=list").forward(req,resp); } /** * 查找 * @param req * @param resp * @throws ServletException */ private void search(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); String hubie=req.getParameter("hubie"); String leixing=req.getParameter("leixing"); String area = req.getParameter("area"); String number = req.getParameter("number"); String name = req.getParameter("name"); String IDcard =req.getParameter("IDcard"); String sex=req.getParameter("sex"); String minzu=req.getParameter("minzu"); String edu=req.getParameter("edu"); List<People> peoples = service.search(hubie,leixing,area,number,name,IDcard,sex,minzu,edu); req.setAttribute("peoples", peoples); req.getRequestDispatcher("searchlist.jsp").forward(req,resp); } }
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <% Object message = request.getAttribute("message"); if(message!=null && !"".equals(message)){ %> <script type="text/javascript"> alert("<%=request.getAttribute("message")%>"); </script> <%} %> <div align="center"> <h1 style="color: black;">人口登记页面</h1> <a href="index.jsp">返回主页</a> <div class="content"> <div class="main"> <form name="form" action="PeopleServlet?method=add" method="post" > <table> <tr> <td>户别:</td> <td> <input type="radio"name="hubie"value="家庭户"checked>家庭户 <input type="radio"name="hubie"value="集体户">集体户 </td> </tr> <tr> <td>住房类型:</td> <td> <input type="radio"name="leixing"value="家庭住宅"checked>家庭住宅 <input type="radio"name="leixing"value="集体住所">集体住所 <input type="radio"name="leixing"value="工作地住所">工作地住所 <input type="radio"name="leixing"value="其他住宅">其他住宅 <input type="radio"name="leixing"value="无住宅">无住宅 </td> </tr> <tr> <td>本户现住房面积</td> <td><input type="text" id="area" name="area" placeholder="输入整数" /></td> </tr> <tr> <td>本户现住房间数</td> <td><input type="text" id="number" name="number" placeholder="输入整数" /></td> </tr> <tr> <td>户主姓名</td> <td><input type="text" id="name" name="name" /></td> </tr> <tr> <td>身份证号</td> <td><input type="text" id="IDcard" name="IDcard" placeholder="13位数字(最后一位可为X)" /></td> </tr> <tr> <td>性别</td> <td> <label for="man" class="radio"> <span class="radio-bg"></span> <input type="radio" name="sex" id="man" value="男" checked="checked" /> 男 <span class="radio-on"></span> </label> <label for="woman" class="radio"> <span class="radio-bg"></span> <input type="radio" name="sex" id="woman" value="女" /> 女 <span class="radio-on"></span> </label> </td> </tr> <tr> <td>民族</td> <td><input type="text" id="minzu" name="minzu" /></td> </tr> <tr> <td>受教育程度</td> <td> <select name="edu" class="shortselect"> <option>研究生</option> <option>大学本科</option> <option>大学专科</option> <option>高中</option> <option>初中</option> <option>小学</option> <option>未上过学</option> </select> </td> </tr> <tr> <td><button type="submit" class="b">提 交<tton></td> </tr> </table> </form> <script type="text/javascript"> function check() { var IDcard = document.getElementById("IDcard");; if(IDcard.value.length!=13){ alert('身份证位数有误!'); IDcard.focus(); return false; } } </script> </div> </div> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <% Object message = request.getAttribute("message"); if(message!=null && !"".equals(message)){ %> <script type="text/javascript"> alert("<%=request.getAttribute("message")%>"); </script> <%} %> <script type="text/javascript"> function check(form) { var name=form.name.value; if(name=="") { alert("名称不能为空"); form.name.focus(); return false; } } </script> <div align="center">删除信息</h> <div class="content"> <div class="main"> <a href="index.jsp">返回主页</a> <br><br> <form name="form" action="PeopleServlet?method=getPeopleByname" method="post" onsubmit="return check(form)"> <a>户主姓名:</a> <input type="text" id="name" name="name" /> <br><br> <input type="submit" value="删除" /> </form> </div> </div> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> <style> .a{ margin-top: 20px; } .b{ font-size: 20px; width: 160px; color: white; background-color: greenyellow; } .tb, td { border: 1px solid black; font-size: 22px; } </style> </head> <body> <div align="center"> <h1 style="color: black;">用户信息删除</h1> <a href="index.jsp">返回主页</a> <table class="tb"> <tr> <td>用户姓名</td> <td>${people.name}</td> </tr> <tr> <td>身份证号</td> <td>${people.IDcard}</td> </tr> <tr> <td>性别</td> <td>${people.sex}</td> </tr> <tr> <td>户别</td> <td>${people.hubie}</td> </tr> <tr> <td>住房类型</td> <td>${people.leixing}</td> </tr> <tr> <td>本户现住房面积</td> <td>${people.area}</td> </tr> <tr> <td>本户现住房间数</td> <td>${people.number}</td> </tr> <tr> <td>受教育程度</td> <td>${people.edu}</td> </tr> </table> <form action="PeopleServlet?method=del" method="post" onsubmit="return check();"> <input type="hidden" id="name" name="name" value="${people.name}"> <input type="submit" name="action1" value="确定删除"> </form> </div> <script type="text/javascript"> function check() { if (confirm("真的要删除吗?")){ return true; }else{ return false; } } </script> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>首页</title> </head> <body> <p>人口普查系统</p> <div align="center"> <div class="a"> <a href="add.jsp">信息登记</a> </div> <div class="a"> <a href="xiugai">信息修改</a> </div> <div class="a"> <a href="del.jsp">信息删除</a> </div> <div class="a"> <a href="search.jsp">查询信息</a> </div> <div class="a"> <a href="PeopleServlet?method=list">人口信息浏览</a> </div> </div> </body> </html>
//list.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <% Object message = request.getAttribute("message"); if(message!=null && !"".equals(message)){ %> <script type="text/javascript"> alert("<%=request.getAttribute("message")%>"); </script> <%} %> <div align="center"> <h1 style="color: black;">人口信息列表</h1> <a href="index.jsp">返回主页</a> <table class="tb"> <tr> <td>id</td> <td>户别</td> <td>住房类型</td> <td>住房间数</td> <td>户主姓名</td> <td>身份证号码</td> <td>性别</td> <td>民族</td> <td>受教育程度</td> <td align="center" colspan="2">操作</td> </tr> <c:forEach items="${peoples}" var="item"> <tr> <td>${item.id }</td> <td>${item.hubie}</td> <td>${item.leixing}</td> <td>${item.number}</td> <td>${item.name}</td> <td>${item.IDcard}</td> <td>${item.sex}</td> <td>${item.minzu}</td> <td>${item.edu}</td> <td><a href="PeopleServlet?method=getcoursebyIDcard&IDcard=${item.IDcard}">修改</a></td> </tr> </c:forEach> </table> </div> </body> </html>
二、遇到的问题
今天的代码基本都是在之前的代码的基础上修改的,好多地方越改越乱,不知道错误在哪里。
三、明日计划
继续完善代码