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

前期准备,主体框架,学生列表显示    请看上一篇文章

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

NO01.在list表中添加一个添加学生连接

  1. <tr >
  2. <td colspan="8"><a href="add.jsp">添加</a></td>
  3. </tr>

NO02.添加一个add.jsp

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <!DOCTYPE html>
  4. <html>
  5. <head>
  6. <meta charset="UTF-8">
  7. <title>添加学生</title>
  8. </head>
  9. <body>
  10. <form method="post" action="AddServlet">
  11. <table border="1">
  12. <tr>
  13. <td>姓名</td>
  14. <td><input type="text" name="sname"></td>
  15. </tr>
  16. <tr>
  17. <td>性别</td>
  18. <td><input type="radio" name="gender" value="男"><input
  19. type="radio" name="gender" value="女"></td>
  20. </tr>
  21. <tr>
  22. <td>电话</td>
  23. <td><input type="text" name="phone"></td>
  24. </tr>
  25. <tr>
  26. <td>生日</td>
  27. <td><input type="text" name="birthday"></td>
  28. </tr>
  29. <tr>
  30. <td>爱好</td>
  31. <td><input type="checkbox" name="hobby" value="篮球">篮球 <input
  32. type="checkbox" name="hobby" value="游泳">游泳 <input
  33. type="checkbox" name="hobby" value="足球">足球 <input
  34. type="checkbox" name="hobby" value="乒乓球">乒乓球 <input
  35. type="checkbox" name="hobby" value="学习">学习</td>
  36. </tr>
  37. <tr>
  38. <td>简介</td>
  39. <td><textarea name="info" rows="3" cols="20"></textarea></td>
  40. </tr>
  41. <tr>
  42. <td colspan="2"><input type="submit" value="添加"></td>
  43. </tr>
  44. </table>
  45. </form>
  46. </body>
  47. </html>

NO03.修改Student类,添加一个有参构造(不需要ID)和一个无参构造

NO04.添加一个增加学生的Servlet

获取表单上提交过来的值然后插入数据库后显示原来的列表

  1. package com.rick.servlet;
  2. import java.io.IOException;
  3. import java.text.ParseException;
  4. import java.text.SimpleDateFormat;
  5. import java.util.Arrays;
  6. import java.util.Date;
  7. import javax.servlet.ServletException;
  8. import javax.servlet.http.HttpServlet;
  9. import javax.servlet.http.HttpServletRequest;
  10. import javax.servlet.http.HttpServletResponse;
  11. import com.rick.been.Student;
  12. import com.rick.sercive.StudentService;
  13. import com.rick.sercive.impl.StudentServiceImpl;
  14. public class AddServlet extends HttpServlet {
  15. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  16. throws ServletException, IOException {
  17. request.setCharacterEncoding("UTF-8");
  18. // 1.获得表提交上来的数据
  19. String sname = request.getParameter("sname");
  20. String gender = request.getParameter("gender");
  21. String phone = request.getParameter("phone");
  22. String birthday = request.getParameter("birthday");
  23. String info = request.getParameter("info");
  24. //String hobby = request.getParameter("hobby");//这个方法只能拿出一个数据
  25. String[] h = request.getParameterValues("hobby");
  26. String hobby = Arrays.toString(h);//得到的h是一个地址,用Arrays.toString转换成字符串
  27. hobby = hobby.substring(1,hobby.length()-1);//但是得到的东西前后会有[]还得用substring去掉前后
  28. // 2、添加到数据库
  29. try {
  30. Date birthdaydate = new SimpleDateFormat("yyyy-mm-dd").parse(birthday);
  31. //在Student类中添加一个无参和一个有参构造
  32. Student student = new Student(sname, gender, phone, birthdaydate, hobby, info);
  33. StudentService service = new StudentServiceImpl();
  34. service.insert(student);
  35. // 3、跳转到列表
  36. //这里直接跳转到页面上,那么这个页面会重新翻译一次,上面那个request的请求存放的数据没有了
  37. //request.getRequestDispatcher("list.jsp").forward(request, response);
  38. request.getRequestDispatcher("StudentListServlet").forward(request, response);
  39. } catch (Exception e) {
  40. // TODO Auto-generated catch block
  41. e.printStackTrace();
  42. }
  43. }
  44. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  45. throws ServletException, IOException {
  46. // TODO Auto-generated method stub
  47. doGet(request, response);
  48. }
  49. }

NO05.修改StudentDao接口,添加一个方法

  1. //添加学生
  2. void insert(Student student) throws SQLException;

NO06.修改StudentDao接口的实现类StudentDaoImpl,添加一个方法

  1. @Override
  2. public void insert(Student student) throws SQLException {
  3. QueryRunner runner = new QueryRunner(JDBCUtil.getDataSouce());
  4. runner.update("insert into stu values(null,?,?,?,?,?,?)",
  5. student.getSname(),student.getGender(),student.getPhone(),
  6. student.getBirthday(),student.getHobby(),student.getInfo());
  7. }

NO07.修改StudentService接口,添加一个方法

  1. //添加
  2. void insert(Student student) throws SQLException;

NO08.修改StudentService接口的实现类StudentServiceImpl,添加一个方法

  1. @Override
  2. public void insert(Student student) throws SQLException {
  3. StudentDao dao = new StudentDaoImpl();
  4. dao.insert(student);
  5. }

MVC学生管理系统-阶段II(添加学生信息)的更多相关文章

  1. MVC学生管理系统-阶段IV(修改学生信息)

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

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

    项目源码 :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. python开发的学生管理系统

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

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

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

  7. 非常简约学生管理系统——HashSet进行编写

    很小的一个练习,可以参考一下啊~~~~~~ 1:注意:学生类中进行多个重要方法的重写 package com.xt.homework; public class Student { private S ...

  8. <每日一题>题目7:简单的学生管理系统V1.0

    ''' # 学生管理系统v1.0 # 添加学生的信息 # 删除学生的信息 # 修改学生的信息 # 查看学生的信息 #遍历学生的信息 #退出系统 ''' import json #1 显示操作功能 de ...

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

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

随机推荐

  1. query.locate过个过滤参数

    需要引用Variants locate( 'typeid;name',vararrayof([key1,key2]),[]);

  2. redis.rpm 安装

    yum install jemalloc wget http://www6.atomicorp.com/channels/atomic/centos/6/x86_64/RPMS/redis-3.0.7 ...

  3. hibernate部分源码解析and解决工作上关于hibernate的一个问题例子(包含oracle中新建表为何列名全转为大写且通过hibernate取数时如何不用再次遍历将列名(key)值转为小写)

    最近在研究系统启动时将数据加载到内存非常耗时,想着是否有办法优化!经过日志打印测试发现查询时间(查询时间:将数据库数据查询到系统中并转为List<Map>或List<*.Class& ...

  4. js 中 一些重要的数组方法

    今天在学Vue的时候,看到了好多JS的数组方法,但是我不知道,我以为是Vue的方法,结果找了半天资料也没找出来,最后才发现这是JS的数组对象方法,于是就想做一下笔记,加深一下印象. Array 对象方 ...

  5. taro中自定义tabbar实现中间图标凸出效果

    遇到的一个需求是在tabbar上有一个凸起的小图标, 但是微信自带的tabbar是没有这个效果的, 无奈,只能使用自定义tabbar,查找了多方文档后发现大多是原生小程序实现, 关于taro文档的少之 ...

  6. html的适配

    html值得一说的应该就是适配 !!适配是与手机同时存在的 写好一个页面在手机端打开,会发现这个页面显示很小,那是因为设备的视口宽度viewport不等于设备宽度device-width,而页面是根据 ...

  7. numpy中的CSV文件

    As we all know,we use numpy to do some data explore.CSV has a good point to get a lot data. so how c ...

  8. Python Sphinx使用踩坑记录

    描述 使用 pip 安装sphinx后,按照教程建立了一个新的py文件,如下 # run.py def run(name): """ this is how we run ...

  9. JAVA DateUtil 工具类封装(转)

    原文链接 https://blog.csdn.net/wangpeng047/article/details/8295623  作者三次整理后的代码 下载链接   https://www.lanzou ...

  10. 《ES6标准入门》(阮一峰)--7.数值的扩展

    1.二进制和八进制表示法 ES6 提供了二进制和八进制数值的新的写法,分别用前缀0b(或0B)和0o(或0O)表示. 0b111110111 === 503 // true 0o767 === 503 ...