本⽂接着,继续为⼤家分享了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\ //跳转
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,调⽤daoStudentDao.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();
List request.setAttribute(\"list\ //3.跳转界⾯--列表界⾯ request.getRequestDispatcher(\"list.jsp\").forward(request, response); } catch (SQLException e) { e.printStackTrace(); } } 例如:查询所有性别为男的学⽣信息; 查询结果如下: 3. 获取数据,调⽤service,调⽤daoStudentDao.java & StudentService.java /** * 模糊查询,根据姓名或者根据性别,或者两者兼有 * @param sname * @param sgender * @return List List StudentDaoImpl.java【这是⼀个需要注意的部分】 @Override public List * 如果只有姓名 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 //判断是否有姓名,如果有,就组拼到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 StudentServiceImol…java @Override public List 以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。 因篇幅问题不能全部显示,请点此查看更多更全内容