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

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

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

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

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

NO02.添加一个add.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>添加学生</title>
</head>
<body>
<form method="post" action="AddServlet">
<table border="1">
<tr>
<td>姓名</td>
<td><input type="text" name="sname"></td>
</tr>
<tr>
<td>性别</td>
<td><input type="radio" name="gender" value="男">男 <input
type="radio" name="gender" value="女">女</td>
</tr>
<tr>
<td>电话</td>
<td><input type="text" name="phone"></td>
</tr>
<tr>
<td>生日</td>
<td><input type="text" name="birthday"></td>
</tr>
<tr>
<td>爱好</td>
<td><input type="checkbox" name="hobby" value="篮球">篮球 <input
type="checkbox" name="hobby" value="游泳">游泳 <input
type="checkbox" name="hobby" value="足球">足球 <input
type="checkbox" name="hobby" value="乒乓球">乒乓球 <input
type="checkbox" name="hobby" value="学习">学习</td>
</tr>
<tr>
<td>简介</td>
<td><textarea name="info" rows="3" cols="20"></textarea></td>
</tr> <tr>
<td colspan="2"><input type="submit" value="添加"></td> </tr>
</table>
</form>
</body>
</html>

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

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

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

package com.rick.servlet;

import java.io.IOException;
import java.text.ParseException;
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 AddServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
// 1.获得表提交上来的数据
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 hobby = request.getParameter("hobby");//这个方法只能拿出一个数据
String[] h = request.getParameterValues("hobby");
String hobby = Arrays.toString(h);//得到的h是一个地址,用Arrays.toString转换成字符串
hobby = hobby.substring(1,hobby.length()-1);//但是得到的东西前后会有[]还得用substring去掉前后
// 2、添加到数据库
try {
Date birthdaydate = new SimpleDateFormat("yyyy-mm-dd").parse(birthday);
//在Student类中添加一个无参和一个有参构造
Student student = new Student(sname, gender, phone, birthdaydate, hobby, info);
StudentService service = new StudentServiceImpl();
service.insert(student);
// 3、跳转到列表
//这里直接跳转到页面上,那么这个页面会重新翻译一次,上面那个request的请求存放的数据没有了
//request.getRequestDispatcher("list.jsp").forward(request, response);
request.getRequestDispatcher("StudentListServlet").forward(request, response);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
} }

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

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

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

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

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

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

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

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

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. arm linux 移植 ffmpeg 库 + x264

    背景 Ffmpeg 中带有h264的解码,没有编码,需要添加x264.libx264是一个自由的H.264编码库,是x264项目的一部分,使用广泛,ffmpeg的H.264实现就是用的libx264. ...

  2. Rabbitmq与spring整合之重要组件介绍——rabbitAdmin组件

    rabbitAdmin组件是一个管理组件,主要是用户通过该组件进行rabbitmq的队列交换器虚拟主机等等进行操作.这里面有些教程说不用声明可以直接绑定,但是本博主运行时,不生命情况下就会报错,可能是 ...

  3. SqlServer查看锁表与解锁

    某些情况下,sqlserver的表会被锁住,比如某个会话窗口有数据一直没提交,窗口又没关闭,这时表就会被锁住 其他任何连接查询表数据时都不会返回 这时需要手工杀掉产生死锁的会话ID,才能恢复正常 查看 ...

  4. 计算机是如何计算的、运行时栈帧分析(神奇i++续)

    关于i++的疑问 通过JVM javap -c 查看字节码执行步骤了解了i++之后,衍生了一个问题: int num1=50; num1++*2执行的是imul(将栈顶两int类型数相乘,结果入栈), ...

  5. 值得一学的C语言

    P1085 不高兴的津津 #include <stdio.h> int main( ) { int a,b; int max=0; int result; for (int i = 0; ...

  6. 049、Java中使用switch判断,不加入break时的操作

    01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...

  7. 传入sql语句,执行完提取内容赋值到控件上

    class procedure DBTools.FillStrings(ComboBoxEh: TDBComboBoxEh; sql: string; Default: Boolean = False ...

  8. android studio (安卓开发)如何使用外部模拟器(mumu模拟器)调试运行程序

    开发安卓 我觉得大家明白自带的模拟器卡的要死而且启动慢(我觉得八核的计算机应该可以解决这个问题),这里使androidstudio 使用外部模拟器 MuMu模拟器  配置方法 eclipse 开发安卓 ...

  9. 抓包工具fiddler的Https证书设置

    一.工具(option)--设置(setting)-- https-- 动作(actions)-- (open windows certificate manger)-- 搜索(fiddler)删除所 ...

  10. Python3 格式化输出

    Python3 格式化输出 今天用字符串功能的时候,我突然忘记了格式化输出的方式X﹏X.所以赶紧恶补一下. 1.打印字符串 print("My name is %s" %(&quo ...