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; // ...
随机推荐
- python内置函数三
ord() 函数 和 chr() 相反 chr() 是将数字转换成assci码 ord() 是将字符串转换成assci码 显示 pow() 函数 pow(x,y,z) 表示x**y ...
- pip install .whl文件时is not a supported wheel on this platform.解决方法
首先,在python中输入import pip和print(pip.pep425tags.get_supported()),从而获取pip支持的文件名和版本. somnus@somnus-HP-Pa ...
- 2-10 就业课(2.0)-oozie:12、cm环境搭建的基础环境准备
8.clouderaManager5.14.0环境安装搭建 Cloudera Manager是cloudera公司提供的一种大数据的解决方案,可以通过ClouderaManager管理界面来对我们的集 ...
- 04.swoole学习笔记--webSocket服务器
<?php //创建webSocket服务器 $serv=); //获取请求 //on //open 建立连接 $serv:服务器 $request:客户端信息 $serv->on('op ...
- 把自己的项目发布到maven仓库并在maven和gradle中开始使用
把自己的项目发布到maven仓库并在maven和gradle中开始使用 上一条博客中提到的日志打印项目总算是维护的差不多了, 不过现在使用它还是打成jar包放到其他项目内, 所以决定把项目传到mave ...
- 012.CI4框架CodeIgniter, 加载并调用自己的Libraries库
01. 在Libraries目录创建一个Mylib文件,内容是一个简单的类 <?php namespace App\Controllers; class Home extends BaseCon ...
- ShowDialog()之后,主窗体失去焦点
开发wince的时候遇到这个问题,简单搞定了. ...... form.ShowDialog(); this.focus();
- php中date('Y/m/d',time())显示不对
一. 时间不对是因为没设置时区 在xampp/php/php.ini中ctrl + f 查找date.timezone 该行默认注释,去掉 ; 修改为 date.timezone = PRC 二 上述 ...
- Delphi IDFtp用法,包含断点续传
1 连接远程服务器procedure Connect(AAutoLogin: boolean; const ATimeout: Integer);2 改变目录procedure ChangeDir ...
- DISCOVERING THE ANTI-VIRUS SIGNATURE AND BYPASSING IT
前言:看了这篇突然想起,2019年刚开始学习的时候在心东的视频教程中,他当时在360的情况下绕Regsvr32跟这篇文章也有点相似,不过这个人的思路更加的广阔! X