JavaWeb学生信息管理系统 JavaWeb实现学生信息管理系统(3)
笑-_-笑 人气:0本文接着第二篇,继续为大家分享了JavaWeb实现学生信息管理系统,供大家参考,具体内容如下
今日任务:实现学生管理系统的删除、更新、修改和模糊查询功能!
一、删除学生信息
点击超链接,弹出一个询问是否删除的对话框,如果点击了确定,删除此学生的信息。
1. 超链接执行一个js方法
<a href="#" rel="external nofollow" onclick="doDelete(${stu.sid})">删除</a>
在doDelete()方法里判断点击的选项,然后跳转到DeleteServlet。
<script type="text/javascript"> function doDelete(sid){ //如果这里弹出的对话框,用户点击的是确定,就去请求servlet var flag = confirm("确定删除?"); if(flag){ //表明确定删除。则访问servlet,在当前标签页上打开超链接 window.location.href = "DeleteServlet?sid="+sid; } } </script>
2. DeleteServlet收到了请求,然后去调用service,service去调用dao。
DeleteServlet.java
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { //1.接受id int sid = Integer.parseInt(request.getParameter("sid")); //2.执行删除 StudentService service = new StudentServiceImpl(); service.delete(sid); //3.跳转到列表页 request.getRequestDispatcher("StudentListServlet").forward(request,response); } catch (SQLException e) { e.printStackTrace(); } }
StudentDao.java & StudentService.java
/** * 根据id删除学生 * @param sid * @throws SQLException */ void delete(int sid) throws SQLException;
StudentDaoImpl.java & StudentServiceImpl.java
public class StudentDaoImpl implements StudentDao { @Override public void delete(int sid) throws SQLException { QueryRunner runner = new QueryRunner(JDBCUtil02.getDataSource()); String sql = "delete from stu where sid=?"; runner.update(sql,sid); } }
public class StudentServiceImpl implements StudentService{ @Override public void delete(int sid) throws SQLException { StudentDao dao = new StudentDaoImpl(); dao.delete(sid); } }
3. 删除结果如下:
删除id = 3的学生的信息
二、更新(修改)学生信息
1. 点击列表上的更新,跳转到EditServlet上
<a href="EditServlet?sid=${stu.sid }" rel="external nofollow" >更新</a>
2. 在EditServlet里面,根据id查询这个学生的所有信息
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { //1.接受id int sid = Integer.parseInt(request.getParameter("sid")); //2.查询学生数据 StudentService service = new StudentServiceImpl(); Student stu = service.findStudentById(sid); //3.显示数据 //存数据 request.setAttribute("stu", stu); //跳转 request.getRequestDispatcher("edit.jsp").forward(request, response); } catch (SQLException e) { e.printStackTrace(); } }
3. 跳转到更新页面edit.jsp,在edit.jsp上显示数据
原来的学生信息如图所示,我们修改编号为2的学生的电话为11111。
点击更新,跳转到该学生的修改页面,如下:
修改页面代码如下:
<body> <h3>更新学生页面</h3> <form action="UpdateServlet" method="post"> <input type="hidden" name="sid" value="${stu.sid }"> <table border="1" width="600"> <tr> <td>姓名</td> <td><input type="text" name="sname" value="${stu.sname }"></td> </tr> <tr> <td>性别</td> <td> <!-- 如果性别是男的,可以在男的性别input标签里面出现checked; 如果性别是女的,可以在女的性别input标签里面出现checked; --> <input type="radio" name="gender" value="男" <c:if test="${stu.gender == '男' }">checked</c:if> >男 <input type="radio" name="gender" value="女" <c:if test="${stu.gender == '女' }">checked</c:if> >女 </td> </tr> <tr> <td>电话</td> <td><input type="text" name="phone" value="${stu.phone }"></td> </tr> <tr> <td>生日</td> <td><input type="text" name="birthday" value="${stu.birthday }"></td> </tr> <tr> <td>爱好</td> <td> <!-- 例如:爱好有篮球,足球,看书 由于爱好有很多歌,里面存在包含关系 --> <input type="checkbox" name="hobby" value="游泳" <c:if test="${fn:contains(stu.hobby,'游泳') }">checked</c:if> >游泳 <input type="checkbox" name="hobby" value="篮球" <c:if test="${fn:contains(stu.hobby,'篮球') }">checked</c:if> >篮球 <input type="checkbox" name="hobby" value="足球" <c:if test="${fn:contains(stu.hobby,'足球') }">checked</c:if> >足球 <input type="checkbox" name="hobby" value="看书" <c:if test="${fn:contains(stu.hobby,'看书') }">checked</c:if> >看书 <input type="checkbox" name="hobby" value="写字" <c:if test="${fn:contains(stu.hobby,'写字') }">checked</c:if> >写字 </td> </tr> <tr> <td>简介</td> <td> <textarea rows="3" cols="20" name="info">${stu.info }</textarea> </td> </tr> <tr> <td colspan="2"><input type="submit" value="更新"></td> </tr> </table> </form> </body>
修改该学生的电话为11111,如下:
4. 修改完数据,提交数据到UpdateServlet
【备:代码与AddServlet代码相似,唯一的不同点就是:提交上来的数据是没有带id的,我们需要手动创建一个隐藏的输入框,在这里面给定id的值,以便提交表单时,带上id。
<input type="hidden" name="sid" value="${stu.sid }">
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); try { //1.获取客户端提交上来的数据 int sid = Integer.parseInt(request.getParameter("sid")); System.out.println(sid); String sname = request.getParameter("sname"); System.out.println(sname); String gender = request.getParameter("gender"); String phone = request.getParameter("phone"); String birthday = request.getParameter("birthday"); //传过来是1989-10-18 String info = request.getParameter("info"); //String hobby = request.getParameter("hobby"); String [] h = request.getParameterValues("hobby"); //[篮球,足球,写字]-----篮球,足球,写字 String hobby = Arrays.toString(h); hobby = hobby.substring(1,hobby.length()-1); //String-------Date Date date = new SimpleDateFormat("yyyy-MM-dd").parse(birthday); Student student = new Student(sid,sname,gender,phone,hobby,info,date); //2.更新数据库数据 StudentService service = new StudentServiceImpl(); service.update(student); //3.跳转界面 request.getRequestDispatcher("StudentListServlet").forward(request, response); } catch (Exception e) { e.printStackTrace(); } }
5. 获取数据,调用service,调用dao
StudentDao.java & StudentService.java
/** * 更新学生信息 * @param student 需要更新的学生数据 * @throws SQLException */ void update(Student student) throws SQLException;
StudentDaoImpl.java
public class StudentDaoImpl implements StudentDao { @Override public void update(Student student) throws SQLException { QueryRunner runner = new QueryRunner(JDBCUtil02.getDataSource()); String sql = "update stu set sname=? , gender=? , phone=? , birthday=? , hobby=? , info=? where sid=?"; runner.update(sql, student.getSname(), student.getGender(), student.getPhone(), student.getBirthday(), student.getHobby(), student.getInfo(), student.getSid() ); } }
StudentServiceImpl.java
public class StudentServiceImpl implements StudentService{ @Override public void update(Student student) throws SQLException { StudentDao dao = new StudentDaoImpl(); dao.update(student); } }
6. 更新(修改)学生信息结果如下:
三、模糊查询
1. 在list.jsp加入进行查询的文本框
<form action="SearchStudentServlet" method="post"> <table border="1" width="700"> <tr> <td colspan="8"> 按姓名查询:<input type="text" name="sname"/> 按性别查询:<select name="sgender"> <option value="">--请选择-- <option value="男">男 <option vale="女">女 </select> <input type="submit" value="查询"> <a href="add.jsp" rel="external nofollow" >添加</a> </td> </tr> </table> </form>
实现效果如下图所示:
2. 在SearchServlet里面,查询符合查询条件的学生信息
代码实现如下:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); try { //1.取到了要查询的关键数据:姓名,性别 String sname = request.getParameter("sname"); String sgender = request.getParameter("sgender"); //2.查询 StudentService service = new StudentServiceImpl(); List<Student> list = service.searchStudent(sname, sgender); request.setAttribute("list", list); //3.跳转界面--列表界面 request.getRequestDispatcher("list.jsp").forward(request, response); } catch (SQLException e) { e.printStackTrace(); } }
例如:查询所有性别为男的学生信息;
查询结果如下:
3. 获取数据,调用service,调用dao
StudentDao.java & StudentService.java
/** * 模糊查询,根据姓名或者根据性别,或者两者兼有 * @param sname * @param sgender * @return List<Student> * @throws SQLException */ List<Student> searchStudent(String sname,String sgender) throws SQLException;
StudentDaoImpl.java【这是一个需要注意的部分!!!】
@Override public List<Student> searchStudent(String sname, String sgender) throws SQLException { QueryRunner runner = new QueryRunner(JDBCUtil02.getDataSource()); /** * 如果只有姓名 select * from stu where sname like?; * 如果只有性别 select * from stu where gender=? * 如果两个都有 select * from stu where sname like? and gender=? * 如果两个都没有就查询所有 */ String sql = "select * from stu where 1=1 "; List<String> list = new ArrayList<String>(); //判断是否有姓名,如果有,就组拼到sql语句里 if(!TestUtils.isEmpty(sname)) { sql = sql + "and sname like ?"; list.add("%"+sname+"%"); } //判断是否有性别,如果有,就组拼到sql语句里 if(!TestUtils.isEmpty(sgender)) { sql = sql + "and gender = ?"; list.add(sgender); } return runner.query(sql, new BeanListHandler<Student>(Student.class),list.toArray()); }
StudentServiceImol…java
@Override public List<Student> searchStudent(String sname, String sgender) throws SQLException { StudentDao dao = new StudentDaoImpl(); return dao.searchStudent(sname, sgender); }
加载全部内容