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%20}" 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"/>
     &nbsp;&nbsp;&nbsp;
     按性別查詢:<select name="sgender">
         <option value="">--請選擇--
         <option value="男">男
         <option vale="女">女
        </select>
     &nbsp;&nbsp;&nbsp; 
     <input type="submit" value="查詢">
     &nbsp;&nbsp;&nbsp;
     <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);
 }

以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀: