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; // ...
随机推荐
- php: 文件上传
1.主页: <!DOCTYPE html><html lang="en"><head> <meta charset="UT ...
- 1-8SpringBoot之切面AOP
SpringBoot提供了强大AOP支持,我们前面讲解过AOP面向切面,所以这里具体AOP原理就补具体介绍: AOP切面主要是切方法,我们一般搞一些日志分析和事务操作,要用到切面,类似拦截器: @As ...
- SciPy 线性代数
章节 SciPy 介绍 SciPy 安装 SciPy 基础功能 SciPy 特殊函数 SciPy k均值聚类 SciPy 常量 SciPy fftpack(傅里叶变换) SciPy 积分 SciPy ...
- 最新获取 QQ头像 和 昵称接口
网上找来的测试可用... 获取QQ头像 http://q2.qlogo.cn/headimg_dl?bs=QQ号&dst_uin=QQ号&dst_uin=QQ号&;dst_ui ...
- 扩展的Sobel 算子
Custom Extended Sobel Filters https://arxiv.org/pdf/1910.00138.pdf sobel算子是进行边缘检测的一个重要算子.它通常是一个3x3的 ...
- shell教程——bash入门
创建shell文件 vim test.sh 写内容 #!/bin/bash echo "Hello World !" 使脚本具有执行权限 chmod +x ./test.sh 执行 ...
- 2-10 就业课(2.0)-oozie:11、hadoop的federation(联邦机制,了解一下)
==================================================== Hadoop Federation 背景概述 单NameNode的架构使得HDFS在集群扩展性 ...
- php+ajax 实现无限树列表
首先介绍我实现的是xhprof插件的日志转为无限树状图,先看效果图: 废话不多说,直接看代码:(辛辛苦苦敲了好久才搞定,逻辑比较多,新手多揣摩) 控制器: 1 <?php 2 3 namespa ...
- Solr查询和过滤器执行顺序剖析
一.简介 Solr的搜索主要由两个操作组成:找到与请求参数相匹配的文档:对这些文档进行排序,返回最相关的匹配文档.默认情况下,文档根据相关度进行排序.这意味着,找到匹配的文档集之后,需要另一个操作来计 ...
- 017、Java中使用float型
01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...