本文接着第二篇,继续为大家分享了JavaWeb实现学生信息管理系统,供大家参考,具体内容如下
今日任务:实现学生管理系统的删除、更新、修改和模糊查询功能!
一、删除学生信息
点击超链接,弹出一个询问是否删除的对话框,如果点击了确定,删除此学生的信息。
1. 超链接执行一个js方法
在doDelete()方法里判断点击的选项,然后跳转到DeleteServlet。
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上
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。
点击更新,跳转到该学生的修改页面,如下:
修改页面代码如下:
更新学生页面
修改该学生的电话为11111,如下:
4. 修改完数据,提交数据到UpdateServlet
【备:代码与AddServlet代码相似,唯一的不同点就是:提交上来的数据是没有带id的,我们需要手动创建一个隐藏的输入框,在这里面给定id的值,以便提交表单时,带上id。
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加入进行查询的文本框
实现效果如下图所示:
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(); Listlist = 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* @throws SQLException */ List searchStudent(String sname,String sgender) throws SQLException;
StudentDaoImpl.java【这是一个需要注意的部分!!!】
@Override public ListsearchStudent(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 list = new ArrayList (); //判断是否有姓名,如果有,就组拼到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.class),list.toArray()); }
StudentServiceImol…java
@Override public ListsearchStudent(String sname, String sgender) throws SQLException { StudentDao dao = new StudentDaoImpl(); return dao.searchStudent(sname, sgender); }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。