MVC学生管理系统-阶段IV(修改学生信息)
项目源码 :https://download.csdn.net/download/weixin_44718300/11091042
前期准备,主体框架,
学生列表显示 请看阶段一文章
添加学生信息 请看阶段二文章
删除学生信息 请看阶段三文章
本文是对阶段一 、二、三的增加部分,不建议跳跃查看
首先,我们捋一下思路,要想修改界面
- 点击修改的时候要获取被修改学生的ID ,知道去修改谁
- 然后拿着ID去数据库中查一下
- 把数据库中的数据拿出来填入表单中(数据回显/回填)
- 然后拿着ID和修改后的数据。进行数据库操作
- 提交到数据库中
- 返回列表
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(修改学生信息)的更多相关文章
- MVC学生管理系统-阶段III(删除学生信息)
项目源码 :https://download.csdn.net/download/weixin_44718300/11091042 前期准备,主体框架,学生列表显示 请看阶段一文章 添加学生信息 ...
- MVC学生管理系统-阶段II(添加学生信息)
项目源码 :https://download.csdn.net/download/weixin_44718300/11091042 前期准备,主体框架,学生列表显示 请看上一篇文章 本文是对阶段 ...
- MVC学生管理系统-阶段V(模糊查询)
项目源码 :https://download.csdn.net/download/weixin_44718300/11091042 此处省略一段话.去上一篇查看 NO01:修改list.jsp < ...
- MVC学生管理系统-阶段I(显示学生列表)
项目源码 :https://download.csdn.net/download/weixin_44718300/11091042 目录 MVC设计模式 前期准备: NO01:新建一个index.js ...
- Winform 学生管理系统增删改查
数据库: create database adonet go use adonet go create table xue ( code ), name ), sex bit, birth datet ...
- Java 简化版学生管理系统(IO版)
Student management system 学生管理系统IO版 详细知识点可参考一下几篇文章 常用API Java 之ArrayList集合及应用 Java 之IO流及应用 Compreh ...
- python开发的学生管理系统
python开发的学生管理系统(基础版) #定义一个函数,显示可以使用的功能列表给用户 def showInfo(): print("-"*30) print(" 学生管 ...
- python简易版学生管理系统
#coding=utf- def showInfo(): print("**************") print(" 学生管理系统") print(&quo ...
- java学生管理系统
student类 package cn.itheima.Manag; /** * *标准类 * **/public class Student { //学号 private String id; // ...
随机推荐
- css 文本溢出省略号
单行溢出显示省略号: white-space: nowrap; overflow: hidden; text-overflow: ellipsis; 多行溢出显示省略号: text-overflow: ...
- Hessian Token权限认证
博客分类: Hessian 添加Token验证,如何生成Token,计算方式如下,采用不可逆转的方式生成[MD5加密]: 服务器端存储Token,采用线程安全的Map 客户端在发送业务请求前,先去 ...
- Java 变参函数的实现
Java的变参函数实现实际上参数是一个数组,其简单用法如下 public class variableParamTest { private static void variableParam(O ...
- syx学习笔记
SYX复活了,在悲痛之际,希望能让自己获得更多的知识,更有进步,所以留此博客 数学 推荐blog: 1 2 原根表 FFT(快速傅里叶变换) 2019/12/05 √ 博客 blog 题目 Q1 NT ...
- OTF格式
OTF, OpenType 字体文件. OpenType也叫Type 2字体,是由Microsoft和Adobe公司开发的另外一种字体格式.它也是一种轮廓字体,比TrueType更为强大,最明显的一个 ...
- 【QSBOJ】组成三位数
题目描述 将1,2,…,9共9个数分成三组,分别组成三个三位数,且使这三个三位数构成1:2:3的比例,试求出所有满足条件的三个三位数. 例如:三个三位数192,384,576满足以上条件. 输出 输出 ...
- 【转载】Jmeter关联-正则表达式提取器
今天研发同事提供了一个验证token的接口,要验证token的正确性,现在将整个过程做如下记录: 场景:验证token的正确性 原理:首先用户登录成功后,会在Response head ...
- vs2010编译C++ 对象的使用
// CTest.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> using names ...
- 2-10 就业课(2.0)-oozie:11、hadoop的federation(联邦机制,了解一下)
==================================================== Hadoop Federation 背景概述 单NameNode的架构使得HDFS在集群扩展性 ...
- 2-10 就业课(2.0)-oozie:4、通过oozie执行shell脚本
oozie的配置文件job.properties:里面主要定义的是一些key,value对,定义了一些变量,这些变量往workflow.xml里面传递workflow.xml :workflow的配置 ...