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; // ...
随机推荐
- vue小程序ref和v-for结合使用得到ref数组的一些问题
项目中需要对每一个民宿里的每一个房间都需要popup弹出层来介绍每一个房间,房间数据都在一个接口(此民宿)上. 主要代码如下: HTML: <view v-for='(item,index) i ...
- 百度地图javascript API,每个功能每天免费的次数
- VUE - vuex state的使用
1,安装 进入项目目录,执行 vue add vuex 命令 2,会在src的目录下新增store文件夹 3,打开store文件夹下的index.js , 给 state 设定一些数据 impor ...
- R 读取回归模型的信息
参考博客: http://blog.sina.com.cn/s/blog_8f5b2a2e0101fmiq.html https://blog.csdn.net/huangyouyu523/artic ...
- Metasploit学习笔记——情报搜集技术(只记录与metasploit有关的)
1.外围信息搜集 1.1whois域名注册信息查询 示例代码如下 msf > whois testfire.net 1.2网站的目录结构 示例代码如下 msf > use auxiliar ...
- html的适配
html值得一说的应该就是适配 !!适配是与手机同时存在的 写好一个页面在手机端打开,会发现这个页面显示很小,那是因为设备的视口宽度viewport不等于设备宽度device-width,而页面是根据 ...
- delphi关闭和禁用Windows服务
function StopServices(const SvrName: string): Boolean; var SCH, SvcSCH: SC_HANDLE; SS: TServiceStatu ...
- JAVA实现--基础算法FOR选择排序
首先 实现简单的选择排序. 简单排序的思路很简单,就是通过遍历(数组的length次)的数组,每次遍历找出最小的放到数组的第一个位置,下次遍历时就不用考虑第0位置的数从第1的位置开始找1到length ...
- 02 DML(DataManipulationLanguage)
1.插入记录 基本语法 : INSERT INTO tbl_name (col_name ,col_name1,..,col_nameN) VALUES (val1,val2, ...
- HTML文本域(文本框)禁止修改写入数据方法
html文本域有时需要禁止修改内容,方法如下: 加入readonly=""或readonly="readonly" 如下:<input name=&quo ...