您好,登錄后才能下訂單哦!
這篇文章主要介紹“JavaWeb實現(xiàn)學(xué)生信息管理系統(tǒng)之三”,在日常操作中,相信很多人在JavaWeb實現(xiàn)學(xué)生信息管理系統(tǒng)之三問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”JavaWeb實現(xiàn)學(xué)生信息管理系統(tǒng)之三”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!
本文接著第二篇,繼續(xù)為大家分享了JavaWeb實現(xiàn)學(xué)生信息管理系統(tǒng),供大家參考,具體內(nèi)容如下
今日任務(wù):實現(xiàn)學(xué)生管理系統(tǒng)的刪除、更新、修改和模糊查詢功能!
點擊超鏈接,彈出一個詢問是否刪除的對話框,如果點擊了確定,刪除此學(xué)生的信息。
1. 超鏈接執(zhí)行一個js方法
<a href="#" rel="external nofollow" onclick="doDelete(${stu.sid})">刪除</a>
在doDelete()方法里判斷點擊的選項,然后跳轉(zhuǎn)到DeleteServlet。
<script type="text/javascript"> function doDelete(sid){ //如果這里彈出的對話框,用戶點擊的是確定,就去請求servlet var flag = confirm("確定刪除?"); if(flag){ //表明確定刪除。則訪問servlet,在當(dāng)前標簽頁上打開超鏈接 window.location.href = "DeleteServlet?sid="+sid; } } </script>
2. DeleteServlet收到了請求,然后去調(diào)用service,service去調(diào)用dao。
DeleteServlet.java
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { //1.接受id int sid = Integer.parseInt(request.getParameter("sid")); //2.執(zhí)行刪除 StudentService service = new StudentServiceImpl(); service.delete(sid); //3.跳轉(zhuǎn)到列表頁 request.getRequestDispatcher("StudentListServlet").forward(request,response); } catch (SQLException e) { e.printStackTrace(); } }
StudentDao.java & StudentService.java
/** * 根據(jù)id刪除學(xué)生 * @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. 刪除結(jié)果如下:
刪除id = 3的學(xué)生的信息
1. 點擊列表上的更新,跳轉(zhuǎn)到EditServlet上
<a href="EditServlet?sid=${stu.sid }" rel="external nofollow" >更新</a>
2. 在EditServlet里面,根據(jù)id查詢這個學(xué)生的所有信息
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { //1.接受id int sid = Integer.parseInt(request.getParameter("sid")); //2.查詢學(xué)生數(shù)據(jù) StudentService service = new StudentServiceImpl(); Student stu = service.findStudentById(sid); //3.顯示數(shù)據(jù) //存數(shù)據(jù) request.setAttribute("stu", stu); //跳轉(zhuǎn) request.getRequestDispatcher("edit.jsp").forward(request, response); } catch (SQLException e) { e.printStackTrace(); } }
3. 跳轉(zhuǎn)到更新頁面edit.jsp,在edit.jsp上顯示數(shù)據(jù)
原來的學(xué)生信息如圖所示,我們修改編號為2的學(xué)生的電話為11111。
點擊更新,跳轉(zhuǎn)到該學(xué)生的修改頁面,如下:
修改頁面代碼如下:
<body> <h4>更新學(xué)生頁面</h4> <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標簽里面出現(xiàn)checked; 如果性別是女的,可以在女的性別input標簽里面出現(xiàn)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> <!-- 例如:愛好有籃球,足球,看書 由于愛好有很多歌,里面存在包含關(guān)系 --> <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>
修改該學(xué)生的電話為11111,如下:
4. 修改完數(shù)據(jù),提交數(shù)據(jù)到UpdateServlet
【備:代碼與AddServlet代碼相似,唯一的不同點就是:提交上來的數(shù)據(jù)是沒有帶id的,我們需要手動創(chuàng)建一個隱藏的輸入框,在這里面給定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.獲取客戶端提交上來的數(shù)據(jù) 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.更新數(shù)據(jù)庫數(shù)據(jù) StudentService service = new StudentServiceImpl(); service.update(student); //3.跳轉(zhuǎn)界面 request.getRequestDispatcher("StudentListServlet").forward(request, response); } catch (Exception e) { e.printStackTrace(); } }
5. 獲取數(shù)據(jù),調(diào)用service,調(diào)用dao
StudentDao.java & StudentService.java
/** * 更新學(xué)生信息 * @param student 需要更新的學(xué)生數(shù)據(jù) * @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. 更新(修改)學(xué)生信息結(jié)果如下:
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>
實現(xiàn)效果如下圖所示:
2. 在SearchServlet里面,查詢符合查詢條件的學(xué)生信息
代碼實現(xiàn)如下:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); try { //1.取到了要查詢的關(guān)鍵數(shù)據(jù):姓名,性別 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.跳轉(zhuǎn)界面--列表界面 request.getRequestDispatcher("list.jsp").forward(request, response); } catch (SQLException e) { e.printStackTrace(); } }
例如:查詢所有性別為男的學(xué)生信息;
查詢結(jié)果如下:
3. 獲取數(shù)據(jù),調(diào)用service,調(diào)用dao
StudentDao.java & StudentService.java
/** * 模糊查詢,根據(jù)姓名或者根據(jù)性別,或者兩者兼有 * @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); }
到此,關(guān)于“JavaWeb實現(xiàn)學(xué)生信息管理系統(tǒng)之三”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。