JavaWeb实现学生信息管理系统(3)
本文接着,继续为大家分享了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); }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
下一篇: 迅雷玩客币如何获得 迅雷玩客币有什么作用