项目源码 :https://download.csdn.net/download/weixin_44718300/11091042

前期准备,主体框架,

学生列表显示  请看阶段一文章

添加学生信息  请看阶段二文章

删除学生信息 请看阶段三文章

本文是对阶段一 、二、三的增加部分,不建议跳跃查看

首先,我们捋一下思路,要想修改界面

  1. 点击修改的时候要获取被修改学生的ID ,知道去修改谁
  2. 然后拿着ID去数据库中查一下
  3. 把数据库中的数据拿出来填入表单中(数据回显/回填)
  4. 然后拿着ID和修改后的数据。进行数据库操作
  5. 提交到数据库中
  6. 返回列表

NO01:修改list.jsp

 <a href="EditServlet?sid=${stu.sid}">更新</a>

NO02:添加Servlet

public class EditServlet extends HttpServlet {

	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) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
} }

NO03:修改sercice和dao

//根据ID查询单个学生对象

Student findStudentById(Integer sid) throws SQLException;

@Override

public Student findStudentById(Integer sid) throws SQLException {

StudentDao dao = new StudentDaoImpl();

return dao.findStudentById(sid);

}

@Override

public Student findStudentById(Integer sid) throws SQLException {

QueryRunner runner = new QueryRunner(JDBCUtil.getDataSouce());

Student student = runner.query("select * from stu where sid = ?", new BeanHandler<Student>(Student.class),sid);

return student;

}

NO04:添加edit.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>修改学生</title>
</head>
<body> <form method="post" action="UpdateServlet">
<input type="hidden" name ="sid" value=${stu.sid }>
<table border="1">
<tr>
<td>姓名</td>
<td><input type="text" name="sname" value="${stu.sname}"></td>
</tr>
<tr>
<td>性别</td>
<td>
<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 name="info" rows="3" cols="20" >${stu.info }</textarea></td>
</tr> <tr>
<td colspan="2"><input type="submit" value="确认修改"></td> </tr>
</table>
</form>
</body>
</html>

NO05:添加UpdateServlet

package com.rick.servlet;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import com.rick.been.Student;
import com.rick.sercive.StudentService;
import com.rick.sercive.impl.StudentServiceImpl; public class UpdateServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
// 1.获得表提交上来的数据
int sid = Integer.parseInt(request.getParameter("sid"));
String sname = request.getParameter("sname");
String gender = request.getParameter("gender");
String phone = request.getParameter("phone");
String birthday = request.getParameter("birthday");
String info = request.getParameter("info");
String[] h = request.getParameterValues("hobby");
String hobby = Arrays.toString(h);
hobby = hobby.substring(1,hobby.length()-1);
// 2、添加到数据库
try {
Date birthdaydate = new SimpleDateFormat("yyyy-mm-dd").parse(birthday);
Student student = new Student(sid,sname, gender, phone, birthdaydate, hobby, info);
StudentService service = new StudentServiceImpl();
service.update(student);
// 3、跳转到列表 request.getRequestDispatcher("StudentListServlet").forward(request, response);
} catch (Exception e) { e.printStackTrace();
}
} protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
} }

NO06:在Student中添加一个有参构造,参数为全部

NO07:修改service和dao

//更新

void update(Student student) throws SQLException;

@Override

public void update(Student student) throws SQLException {

StudentDao dao = new StudentDaoImpl();

dao.update(student);

}

@Override

public void update(Student student) throws SQLException {

QueryRunner runner = new QueryRunner(JDBCUtil.getDataSouce());

runner.update("update stu set sname=?,gender=?,phone=?,birthday=?,hobby=?,info=? where sid=?",

student.getSname(),student.getGender(),student.getPhone(),

student.getBirthday(),student.getHobby(),student.getInfo(),

student.getSid());

}

NO08:

NO09:

MVC学生管理系统-阶段IV(修改学生信息)的更多相关文章

  1. MVC学生管理系统-阶段III(删除学生信息)

    项目源码 :https://download.csdn.net/download/weixin_44718300/11091042 前期准备,主体框架,学生列表显示  请看阶段一文章 添加学生信息   ...

  2. MVC学生管理系统-阶段II(添加学生信息)

    项目源码 :https://download.csdn.net/download/weixin_44718300/11091042 前期准备,主体框架,学生列表显示    请看上一篇文章 本文是对阶段 ...

  3. MVC学生管理系统-阶段V(模糊查询)

    项目源码 :https://download.csdn.net/download/weixin_44718300/11091042 此处省略一段话.去上一篇查看 NO01:修改list.jsp < ...

  4. MVC学生管理系统-阶段I(显示学生列表)

    项目源码 :https://download.csdn.net/download/weixin_44718300/11091042 目录 MVC设计模式 前期准备: NO01:新建一个index.js ...

  5. Winform 学生管理系统增删改查

    数据库: create database adonet go use adonet go create table xue ( code ), name ), sex bit, birth datet ...

  6. Java 简化版学生管理系统(IO版)

    Student management system   学生管理系统IO版 详细知识点可参考一下几篇文章 常用API Java 之ArrayList集合及应用 Java 之IO流及应用 Compreh ...

  7. python开发的学生管理系统

    python开发的学生管理系统(基础版) #定义一个函数,显示可以使用的功能列表给用户 def showInfo(): print("-"*30) print(" 学生管 ...

  8. python简易版学生管理系统

    #coding=utf- def showInfo(): print("**************") print(" 学生管理系统") print(&quo ...

  9. java学生管理系统

    student类 package cn.itheima.Manag; /** * *标准类 * **/public class Student { //学号 private String id; // ...

随机推荐

  1. [转载]JDK自带的实用工具——native2ascii.exe

    做Java开发的时候,常常会出现一些乱码,或者无法正确识别或读取的文件,原因是编码方式的不一致.native2ascii是sun java sdk提供的一个工具.用来将别的文本类文件(比如*.txt, ...

  2. VScode禁用alt+key触发菜单栏快捷键

    因为用惯了Mac,突然改回Windows,但是已经习惯了按Command键.所以在Windows下把vscode的快捷键全改成alt+key了.但是Windows的alt+key快捷键就比较烦人了.所 ...

  3. spark动态资源(executor)分配

    spark动态资源调整其实也就是说的executor数目支持动态增减,动态增减是根据spark应用的实际负载情况来决定. 开启动态资源调整需要(on yarn情况下) 1.将spark.dynamic ...

  4. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 图片:缩略图功能

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  5. Linux之关于用户的几个重要配置文件

    . /etc/skel/ 用来存放新用户配置文件(环境变量)的目录,当创建新用户时,系统会把当下目录的所有文件拷贝一份到新用户家目录中,即:cp -a /etc/skel/* /home/ddu 2. ...

  6. linux(centos6.9)下使用yum安装mysql,及启动MySQL等

    查看系统自带的mysql版本:rpm -qa | grep mysql 卸载mysql:rpm -e mysql-libs-5.1.73-8.el6_8.x86_64 --nodeps 1. 安装my ...

  7. MySQL导出数据库和导入数据库

    一.导出: 语法:mysqldump --default-character-set=utf8 -u用户名 -p密码 数据库名 -d --add-drop-table > 导出文件名.sql 注 ...

  8. BGR to RGB排列

    BGR to RGB排列 2012年09月27日 13:59:48 雷电羊 阅读数:4759   https://blog.csdn.net/cjsycyl/article/details/80247 ...

  9. 安卓平分位置layout_weight学习记录

    weight (权重)  使用把layout_width写成0   weight 为平分剩余父容器位置 两个标签平分 <?xml version="1.0" encoding ...

  10. 抓包工具fiddler的Https证书设置

    一.工具(option)--设置(setting)-- https-- 动作(actions)-- (open windows certificate manger)-- 搜索(fiddler)删除所 ...