欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JavaWeb實(shí)現(xiàn)學(xué)生信息管理系統(tǒng)(2)

 更新時(shí)間:2021年09月10日 11:56:06   作者:笑-_-笑  
這篇文章主要介紹了JavaWeb實(shí)現(xiàn)學(xué)生信息管理系統(tǒng)的第二篇,實(shí)現(xiàn)學(xué)生管理系統(tǒng)的查找和添加功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文接著上一篇,繼續(xù)為大家分享了JavaWeb實(shí)現(xiàn)學(xué)生信息管理系統(tǒng)的第二篇,供大家參考,具體內(nèi)容如下

今日任務(wù):實(shí)現(xiàn)學(xué)生管理系統(tǒng)的查找和添加功能!

一、查詢學(xué)生信息

1. index.jsp

先寫一個(gè)JSP頁面【W(wǎng)ebContent/index.jsp】,里面放一個(gè)超鏈接

<a href="StudentListServlet" rel="external nofollow" >顯示所有學(xué)生列表</a>

2. StudentListServlet.java

寫StudentListServlet【com.servlet包下的StudentListServlet.java】,接受請求,去調(diào)用service,再由service調(diào)用dao

package com.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.domain.Student;
import com.service.StudentService;
import com.service.impl.StudentServiceImpl;

/**
 * 
 * 負(fù)責(zé)查詢所有的學(xué)生信息,然后呈現(xiàn)到list.jsp頁面上
 *
 */
@WebServlet("/StudentListServlet")
public class StudentListServlet extends HttpServlet {
 
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  
  
  try {
   //1.查詢出來所有的學(xué)生
   StudentService service = new StudentServiceImpl();
   List<Student> list = service.findAll();
   
   //2.先把數(shù)據(jù)存儲到作用域中 
   //3..跳轉(zhuǎn)頁面
   
  } catch (SQLException e) {
  
   e.printStackTrace();
  }
  
 }

 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  doGet(request, response);
 }

}

3. StudentDao.java & StudentDaoImpl.java

3.1 StudentDao.java【com.dao包下】

package com.dao;

import java.sql.SQLException;
import java.util.List;

import com.domain.Student;

/**
 * 這是針對學(xué)生表的數(shù)據(jù)訪問
 * @author Administrator
 *
 */
public interface StudentDao {
 /**
  * 查詢所有學(xué)生
  * @return  List<Student>
  */
 List<Student> findAll() throws SQLException;
}

3.2 StudentDaoImpl.java【com.dao.impl包下】

實(shí)現(xiàn)StudentDao里的findAll()方法。

public class StudentDaoImpl implements StudentDao {
 /**
  * 查詢所有學(xué)生
  * @throws SQLException 
  */
 @Override
 public List<Student> findAll() throws SQLException {
  
  QueryRunner runner = new QueryRunner(JDBCUtil02.getDataSource());
  String sql = "select * from stu";
  List<Student> list = runner.query(sql, new BeanListHandler<Student>(Student.class));
  return list;
 }
}

4. StudentService.java 和 StudentService.java

4.1 StudentService.java

代碼同StudentDao.java,

public interface StudentService {

 /**
  * 查詢所有學(xué)生
  * @return   List<Student>
  * 
  */
 List<Student> findAll() throws SQLException;

}

4.2 StudentService.java

public class StudentServiceImpl implements StudentService{

 @Override
 public List<Student> findAll() throws SQLException {
  StudentDao dao = new StudentDaoImpl();
  return dao.findAll();
 } 
}

5. 在StudentListServlet存儲數(shù)據(jù),并作出頁面響應(yīng)

//2.先把數(shù)據(jù)存儲到作用域中
request.setAttribute("list", list);
   
//3..跳轉(zhuǎn)頁面
request.getRequestDispatcher("list.jsp").forward(request, response);

6. list.jsp

在list.jsp【W(wǎng)ebContent/list.jsp】上顯示數(shù)據(jù)。

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>    
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>學(xué)生列表頁面 </title>
</head>
<body>
 <table border="1" width="700">
  
  <tr>
   <td colspan="8">
    <a href="add.jsp" rel="external nofollow" >添加</a>
   </td>
  </tr>
  
  <tr align="center">
   <td>編號</td>
   <td>姓名</td>
   <td>性別</td>
   <td>電話</td>
   <td>生日</td>
   <td>愛好</td>
   <td>簡介</td>
   <td>操作</td>
  </tr>
  
  <c:forEach items="${list }" var="stu">
   <tr align="center">
   <td>${stu.sid }</td>
   <td>${stu.sname }</td>
   <td>${stu.gender }</td>
   <td>${stu.phone }</td>
   <td>${stu.birthday }</td>
   <td>${stu.hobby }</td>
   <td>${stu.info }</td>
   <td><a href="#" rel="external nofollow"  rel="external nofollow" >更新</a>  <a href="#" rel="external nofollow"  rel="external nofollow" >刪除</a></td>
  </tr>
  </c:forEach>
  
 </table>
</body>
</html>

7. 查找結(jié)果如下:

二、添加學(xué)生信息

1. add.jsp

我們需要先跳轉(zhuǎn)到增加頁面,編寫增加頁面add.jsp【W(wǎng)ebContent/add.jsp】

<body>

 <h3>添加學(xué)生頁面</h3>
 
 <form action="AddServlet" method="post">
  <table border="1" width="600">
   <tr>
    <td>姓名</td>
    <td><input type="text" name="sname"></td>
   </tr>
   <tr>
    <td>性別</td>
    <td>
     <input type="radio" name="gender" value="男">男
     <input type="radio" name="gender" value="女">女
    </td>
   </tr>
   <tr>
    <td>電話</td>
    <td><input type="text" name="phone"></td>
   </tr>
   <tr>
    <td>生日</td>
    <td><input type="text" name="birthday"></td>
   </tr>
   <tr>
    <td>愛好</td>
    <td>
     <input type="checkbox" name="hobby" value="游泳">游泳
     <input type="checkbox" name="hobby" value="籃球">籃球
     <input type="checkbox" name="hobby" value="足球">足球
     <input type="checkbox" name="hobby" value="看書">看書
     <input type="checkbox" name="hobby" value="寫字">寫字
    </td>
   </tr>
   <tr>
    <td>簡介</td>
    <td>
     <textarea rows="3" cols="20" name="info"></textarea>
    </td>
   </tr>
   <tr>
    <td colspan="2"><input type="submit" value="添加"></td>
   </tr>
  </table>
 </form>
 
</body>

實(shí)現(xiàn)結(jié)果如下:

2. AddServlet.java

點(diǎn)擊添加,提交數(shù)據(jù)到AddServlet,處理數(shù)據(jù)。
【備:com.servlet包下的AddServlet.java】

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  
  request.setCharacterEncoding("utf-8");
  
  try {

   //1.獲取客戶端提交上來的數(shù)據(jù)
   String sname = request.getParameter("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);
   
   //2.添加到數(shù)據(jù)庫
   
   //String-------Date
   Date date = new SimpleDateFormat("yyyy-MM-dd").parse(birthday);
   
   Student student = new Student(sname,gender,phone,hobby,info,date);
   StudentService service = new StudentServiceImpl();
   service.insert(student);
   
   //3.跳轉(zhuǎn)到列表頁
   //這里是直接跳轉(zhuǎn)到頁面上,那么這個(gè)頁面會重新翻譯一次,上面那個(gè)request里面的數(shù)據(jù)就沒有了
   //request.getRequestDispatcher("list.jsp").forward(request, response);
   
   //servlet除了能跳jsp之外,還能跳servlet
   request.getRequestDispatcher("StudentListServlet").forward(request, response);
   
   
  } catch (Exception e) {
   e.printStackTrace();
  }
   
 }

3. StudentDao & StudentService

 /**
  * 添加學(xué)生
  * @param student  需要添加到數(shù)據(jù)庫的學(xué)生對象
  * @throws SQLException
  */
 void insert(Student student) throws SQLException;

4. Dao & Service的實(shí)現(xiàn)

4.1 StudentDaoImpl.java

 @Override
 public void insert(Student student) throws SQLException {
  
  QueryRunner runner = new QueryRunner(JDBCUtil02.getDataSource());
  String sql = "insert into stu values(null,?,?,?,?,?,?)";
  runner.update(sql,
    student.getSname(),
    student.getGender(),
    student.getPhone(),
    student.getBirthday(),
    student.getHobby(),
    student.getInfo()
    
    );
 }

4.2 StudentServiceImpl.java

@Override
 public void insert(Student student) throws SQLException {
  StudentDao dao = new StudentDaoImpl();
  dao.insert(student);
  
 }

5. 注意

完成了上述存儲工作之后,需要跳轉(zhuǎn)到列表頁面,這里不能直接跳轉(zhuǎn)到列表頁面,否則沒有什么內(nèi)容顯示。應(yīng)該先跳轉(zhuǎn)到查詢所有學(xué)生信息的那個(gè)servlet,即StudentListServlet,再由這個(gè)servlet跳轉(zhuǎn)到列表頁面。

request.getRequestDispatcher("StudentListServlet").forward(request, response);

hobby的value有多個(gè)值。處理時(shí)需多次轉(zhuǎn)化:

//String hobby = request.getParameter("hobby");
String [] h = request.getParameterValues("hobby");
//[籃球,足球,寫字]-----籃球,足球,寫字
String hobby = Arrays.toString(h);
hobby = hobby.substring(1,hobby.length()-1);

6. 添加結(jié)果如下:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 深度deepin安裝以及jdk、tomcat、Nginx安裝教程

    深度deepin安裝以及jdk、tomcat、Nginx安裝教程

    這篇文章主要給大家介紹了關(guān)于深度deepin安裝以及jdk、tomcat、Nginx安裝的相關(guān)資料,按照文中介紹的方法可以輕松的實(shí)現(xiàn)安裝,對大家的工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2024-01-01
  • Java實(shí)現(xiàn)員工管理系統(tǒng)

    Java實(shí)現(xiàn)員工管理系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)員工管理系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • 深入解析Java類加載的案例與實(shí)戰(zhàn)教程

    深入解析Java類加載的案例與實(shí)戰(zhàn)教程

    本篇文章主要介紹Tomcat類加載器架構(gòu),以及基于類加載和字節(jié)碼相關(guān)知識,去分析動態(tài)代理的原理,對Java類加載相關(guān)知識感興趣的朋友一起看看吧
    2022-05-05
  • 詳解配置spring-boot-actuator時(shí)候遇到的一些小問題

    詳解配置spring-boot-actuator時(shí)候遇到的一些小問題

    這篇文章主要介紹了詳解配置spring-boot-actuator時(shí)候遇到的一些小問題,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • Java實(shí)現(xiàn)發(fā)送郵件并攜帶附件

    Java實(shí)現(xiàn)發(fā)送郵件并攜帶附件

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)發(fā)送郵件并攜帶附件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • SpringBoot整個(gè)啟動過程的分析

    SpringBoot整個(gè)啟動過程的分析

    今天小編就為大家分享一篇關(guān)于SpringBoot整個(gè)啟動過程的分析,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2019-03-03
  • java模擬客戶端向服務(wù)器上傳文件

    java模擬客戶端向服務(wù)器上傳文件

    這篇文章主要為大家詳細(xì)介紹了java模擬客戶端向服務(wù)器上傳文件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-10-10
  • MyBatisPlus數(shù)據(jù)權(quán)限控制實(shí)現(xiàn)的三種方式

    MyBatisPlus數(shù)據(jù)權(quán)限控制實(shí)現(xiàn)的三種方式

    數(shù)據(jù)權(quán)限是保障數(shù)據(jù)安全的重要手段,本文主要介紹了MyBatisPlus數(shù)據(jù)權(quán)限控制實(shí)現(xiàn)的三種方式,具有一定的參考價(jià)值,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-05-05
  • SpringBoot Redis實(shí)現(xiàn)接口冪等性校驗(yàn)方法詳細(xì)講解

    SpringBoot Redis實(shí)現(xiàn)接口冪等性校驗(yàn)方法詳細(xì)講解

    這篇文章主要介紹了SpringBoot Redis實(shí)現(xiàn)接口冪等性校驗(yàn)方法,近期一個(gè)老項(xiàng)目出現(xiàn)了接口冪等性校驗(yàn)問題,前端加了按鈕置灰,依然被人拉著接口參數(shù)一頓輸出,還是重復(fù)調(diào)用了接口,通過復(fù)制粘貼,完成了后端接口冪等性調(diào)用校驗(yàn)
    2022-11-11
  • 基于SpringMVC對接前端參數(shù)注解

    基于SpringMVC對接前端參數(shù)注解

    這篇文章主要介紹了基于SpringMVC對接前端參數(shù)注解的使用,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-09-09

最新評論