JDBC-简单的学生管理系统-增删改查

时间:2022-02-25 17:06:06

结构:三层架构--mvc 

三层是为了解决整个应用程序中各个业务操作过程中不同阶段的代码封装的问题,为了使程序员更加专注的处理某阶段的业务逻辑。

比如将数据库操作代码封装到一层中,提供一些方法根据参数直接返回用户需要的相应数据,这样在处理具体的业务逻辑的时候,就不用关心数据的存储问题了。

MVC主要是为了解决应用程序用户界面的样式替换问题,把展示数据的 HTML 页面尽可能的和业务代码分离。MVC把纯净的界面展示逻辑(用户界面)独立到一些文件中(Views),把一些和用户交互的程序逻辑(Controller)单独放在一些文件中,在 Views 和 Controller 中传递数据使用一些专门封装数据的实体对象,这些对象,统称为Models。

界面包括:index.jsp首页 , list.jsp列表, add.jsp 添加页面, update.jsp修改页面

servlet: Controller.java  servlet控制

formbean: StudentFormBean.java 页面数据封装的bean

utils : JdbcUtils.java 连接数据库, WebTools.java 提供唯一的id, WebUtils.java封装页面数据的类

service: StudentService.java  连接数据库和页面的接口,    StudentServiceImpl.java 接口的实现

dao:   StudentDao.java  调用数据库的接口,    StudentDaoImpl.java  接口的实现

Bean:  Student.java   学生类


 涉及到的知识点有:1.请求转发和请求重定向的区别; 2. 特殊字符在页面中传输时的乱码问题。


 

Student.java

package student.bean;

import java.io.Serializable;
import java.util.Date;

public class Student implements Serializable{   //序列化接口,表示可以在网络中传输

    private String id;
    private String name;
    private String gender;
    private Date birthday;
    private String cellphone;
    private String email;
    private String hobby;
    private String type;
    private String description;
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getGender() {
        return gender;
    }
    public void setGender(String gender) {
        this.gender = gender;
    }
    public Date getBirthday() {
        return birthday;
    }
    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
    public String getCellphone() {
        return cellphone;
    }
    public void setCellphone(String cellphone) {
        this.cellphone = cellphone;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getHobby() {
        return hobby;
    }
    public void setHobby(String hobby) {
        this.hobby = hobby;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    @Override
    public String toString() {
        return "Student [id=" + id + ", name=" + name + ", gender=" + gender
                + ", birthday=" + birthday + ", cellphone=" + cellphone
                + ", email=" + email + ", hobby=" + hobby + ", type=" + type
                + ", description=" + description + "]";
    }  
}

StudentDao.java

package student.dao;

import java.util.List;

import student.bean.Student;

public interface StudentDao {

    /**
     * 添加一个学生
     * @param student
     * @return
     */
    public boolean add(Student student);
    
    /**
     * 删除一个学生
     * @param id
     * @return boolean
     */
    public boolean delete(String id);
    
    /**
     * 更新
     * @param student
     * @return
     */
    public boolean update(Student student);
    
    /**
     * 获取所有的学生
     * @return
     */
    public List<Student> getAllStudent();
    
    /**
     * 根据客户的编号查询客户
     * @param id
     * @return 成功则返回此用户,否则返回null
     */
    public Student findStudentById(String id);
}

StudentDaoImpl.java

package student.dao.impl;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

//import com.mysql.jdbc.PreparedStatement;


import student.bean.Student;
import student.dao.StudentDao;
import student.utils.JdbcUtils;

public class StudentDaoImpl implements StudentDao {

    @Override
    public boolean add(Student student) {
        //拿到连接对象
        Connection conn = JdbcUtils.getConnection();
        //创建预处理命令对象
        PreparedStatement pstmt = null;
        int n = 0;
        try {
            pstmt = conn.prepareStatement("insert into student(id,name,gender,birthday,cellphone,email,hobby,type,description) "
                    + "values(?,?,?,?,?,?,?,?,?)");
            pstmt.setString(1, student.getId());
            pstmt.setString(2, student.getName());
            pstmt.setString(3, student.getGender());
            pstmt.setDate(4, new java.sql.Date(student.getBirthday().getTime()));
            pstmt.setString(5, student.getCellphone());
            pstmt.setString(6, student.getEmail());
            pstmt.setString(7, student.getHobby());
            pstmt.setString(8, student.getType());
            pstmt.setString(9, student.getDescription());
            
            n = pstmt.executeUpdate();
            System.out.println("插入语句执行结果-----" + n);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            //此处是null,没有使用Resultset
            JdbcUtils.release(null, pstmt, conn);
        }
        return n > 0 ? true : false;
    }

    @Override
    public boolean delete(String id) {
        //拿到连接对象
                Connection conn = JdbcUtils.getConnection();
                //创建预处理命令对象
                PreparedStatement pstmt = null;
                int n = 0;
                try {
                    pstmt = conn.prepareStatement("delete from student where id = ?");
                    pstmt.setString(1, id);
                    n = pstmt.executeUpdate();
                    System.out.println("删除语句执行结果-----" + n);
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }finally{
                    JdbcUtils.release(null, pstmt, conn);
                }
                return n > 0 ? true : false;
    }

    @Override
    public boolean update(Student student) {
        //拿到连接对象
                Connection conn = JdbcUtils.getConnection();
                //创建预处理命令对象
                PreparedStatement pstmt = null;
                int n = 0;
                try {
                    pstmt = conn.prepareStatement("update student set name=?,gender=?,birthday=?,cellphone=?,email=?,hobby=?,type=?,description=? "
                            + "where id=?");
                    
                    pstmt.setString(1, student.getName());
                    pstmt.setString(2, student.getGender());
                    pstmt.setDate(3, new java.sql.Date(student.getBirthday().getTime()));
                    pstmt.setString(4, student.getCellphone());
                    pstmt.setString(5, student.getEmail());
                    pstmt.setString(6, student.getHobby());
                    pstmt.setString(7, student.getType());
                    pstmt.setString(8, student.getDescription());
                    pstmt.setString(9, student.getId());
                    
                    n = pstmt.executeUpdate();
                    System.out.println("插入语句执行结果-----" + n);
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }finally{
                    JdbcUtils.release(null, pstmt, conn);
                }
                return n > 0 ? true : false;
    }

    @Override
    public List<Student> getAllStudent() {
        //拿到连接对象
        Connection conn = JdbcUtils.getConnection();
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        List<Student> list = new ArrayList<Student>();
        //创建预处理命令对象
        
        try {
            pstmt = conn.prepareStatement("select id,name,gender,birthday,cellphone,email,hobby,type,description from student");
            //执行sql语句
            rs = pstmt.executeQuery();
            System.out.println("---rs---" + rs);
            while(rs.next()){
                //封装数据
                Student s = new Student();
                try {
                    //由于通过加密生成的id含有特殊符号,在传输到页面上的时候特殊符号不能正常显示,所以会产生无法删除用户的问题
// 所以需要指定编码
String id = URLEncoder.encode(rs.getString("id"),"UTF-8"); s.setId(id); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } s.setName(rs.getString("name")); s.setGender(rs.getString("gender")); s.setBirthday(rs.getDate("birthday")); s.setCellphone(rs.getString("cellphone")); s.setEmail(rs.getString("email")) ; s.setHobby(rs.getString("hobby")) ; s.setType(rs.getString("type")) ; s.setDescription(rs.getString("description")) ; list.add(s); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ JdbcUtils.release(rs, pstmt, conn); } return list; } @Override public Student findStudentById(String id) { //拿到连接对象 Connection conn = JdbcUtils.getConnection(); PreparedStatement pstmt = null; ResultSet rs = null; Student s = new Student(); // List<Student> list = new ArrayList<Student>(); //创建预处理命令对象 try { pstmt = conn.prepareStatement("select name,gender,birthday,cellphone,email,hobby,type,description from student where id='" + id +"' "); //执行sql语句 rs = pstmt.executeQuery(); System.out.println("---FindstudentById---" + rs); while(rs.next()){ //封装数据 s.setId(id); s.setName(rs.getString("name")); s.setGender(rs.getString("gender")); s.setBirthday(rs.getDate("birthday")); s.setCellphone(rs.getString("cellphone")); s.setEmail(rs.getString("email")) ; s.setHobby(rs.getString("hobby")) ; s.setType(rs.getString("type")) ; s.setDescription(rs.getString("description")) ; } } catch (SQLException e) { e.printStackTrace(); }finally{ JdbcUtils.release(null, pstmt, conn); } return s; } }

 StudentService.java   其实就是把StudentDao接口复制过来。

package student.service;

import java.util.List;

import student.bean.Student;

public interface StudentService {

    public boolean add(Student student);
    
    public boolean update(Student student);
    
    public boolean delete(String id);
    
    public List<Student> getAllStudent();
    
    public Student findStudentById(String id);
}

StudentServiceImpl.java  

package student.service.impl;

import java.util.List;

import student.bean.Student;
import student.dao.StudentDao;
import student.dao.impl.StudentDaoImpl;
import student.service.StudentService;

public class StudentServiceImpl implements StudentService {

    StudentDao sd = new StudentDaoImpl();
    @Override
    public boolean add(Student student) {
        // TODO Auto-generated method stub
        return sd.add(student);
    }

    @Override
    public boolean update(Student student) {
        return sd.update(student);
    }

    @Override
    public boolean delete(String id) {
        return sd.delete(id);
    }

    @Override
    public List<Student> getAllStudent() {
        return sd.getAllStudent();
    }

    @Override
    public Student findStudentById(String id) {
        return sd.findStudentById(id);
    }

}

JdbcUtils.java

package student.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;

//数据库的工具类
public class JdbcUtils {

    private static String driverClass = "";
    private static String url = "";
    private static String user="";
    private static String password = "";
    
    static{
        ResourceBundle rb = ResourceBundle.getBundle("dbcfg");
        driverClass = rb.getString("driverClass");
        url = rb.getString("url");
        user = rb.getString("user");
        password = rb.getString("password");
        
        System.out.println("--driverclass--"+driverClass + "@@@url@@@"+url +"###user###"+user+"***password***"+password);
        try {
            Class.forName(driverClass);
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    public static Connection getConnection(){
        try {
            System.out.println("^^^^^^^^^^^^herer^^^^^^^^^^^^^^");
            return DriverManager.getConnection(url,user,password);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
     //释放的时候要从小到大释放
//Connection -> Statement --> Resultset

 

public static void release(ResultSet rs,Statement stmt, Connection conn){
        if(rs != null){
            try {
                rs.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if(stmt != null){
            try {
                stmt.close() ;
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
        if(conn != null){
            try {
                conn.close() ;
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }  
    }  
}

WebTools.java

package student.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;
import sun.misc.BASE64Encoder;

public class WebTools {

    //获取一个新的唯一的id
    public static String createNewId(){
        /**
         * MessageDigest 类为应用程序提供信息摘要算法的功能,如 MD5 或 SHA 算法。信息摘要是安全的单向哈希函数,
         * 它接收任意大小的数据,并输出固定长度的哈希值。
         * 
         */
        String id = UUID.randomUUID().toString();
        MessageDigest md;
        try {
            md = MessageDigest.getInstance("md5");
            //使用指定的 byte 数组对摘要进行最后更新,然后完成摘要计算。
            //public byte[] digest(byte[] input)
            byte[] bs = md.digest(id.getBytes());
            /**
             * 一直以来Base64的加密解密都是使用sun.misc包下的BASE64Encoder及BASE64Decoder的sun.misc.BASE64Encoder/BASE64Decoder类。这人个类是sun公司的内部方法,并没有在java api中公开过,不属于JDK标准库范畴,但在JDK中包含了该类,可以直接使用。但是在eclipse和MyEclipse中直接使用,却找不到该类
             * http://blog.sina.com.cn/s/blog_5a6efa330102v8st.html
             */
            BASE64Encoder base = new BASE64Encoder();
            id = base.encode(bs);
        } catch (NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return id;
        
    }
}

WebUtils.java

package student.utils;

import java.lang.reflect.InvocationTargetException;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtils;

//封装页面数据
public class WebUtils {

    //泛型
    public static <T> T fillFormBean(Class<T> clazz, HttpServletRequest request){
        T t = null;
        try {
            t=clazz.newInstance();
            BeanUtils.populate(t, request.getParameterMap());
        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return t;
    }
}

StudentFormBean.java    FromBean写成了 FromBean

package student.web.formbean;

import java.io.Serializable;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class StudentFromBean implements Serializable{
    private String id;
    private String name;
    private String gender;
    private String birthday;
    private String cellphone;
    private String email;
    private String[] hobby;
    private String type;
    private String description;
    
    private Map<String,String> errors = new HashMap<String, String>();

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getBirthday() {
        return birthday;
    }

    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }

    public String getCellphone() {
        return cellphone;
    }

    public void setCellphone(String cellphone) {
        this.cellphone = cellphone;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String[] getHobby() {
        return hobby;
    }

    public void setHobby(String[] hobby) {
        this.hobby = hobby;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Map<String, String> getErrors() {
        return errors;
    }

    public void setErrors(Map<String, String> errors) {
        this.errors = errors;
    }
    /**
     * 服务端验证数据
     * @return
     */
    public boolean validate(){
        
        //验证姓名
        if(name == "" || name == null){
            errors.put("name", "用户名或密码不能为空");
        }else {
            if(name.length() < 3 || name.length() > 8){
                errors.put("name","名字长度在3-8之间");
            }
        }
        /**
         * 其他的验证稍后再进行完善
         */
        //如果errors为空,说明都是正确的
        return errors.isEmpty();
    }

    @Override
    public String toString() {
        return "StudentFromBean [id=" + id + ", name=" + name + ", gender="
                + gender + ", birthday=" + birthday + ", cellphone="
                + cellphone + ", email=" + email + ", hobby="
                + Arrays.toString(hobby) + ", type=" + type + ", description="
                + description + ", errors=" + errors + "]";
    }
    

}

Controller.java

package student.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.locale.converters.DateLocaleConverter;

import student.bean.Student;
import student.service.StudentService;
import student.service.impl.StudentServiceImpl;
import student.utils.WebTools;
import student.utils.WebUtils;
import student.web.formbean.StudentFromBean;

public class Controller extends HttpServlet {

    StudentService ss = new StudentServiceImpl();
    /**
     * The doGet method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to get.
     * 
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        //拿到页面传递的数据
        String op = request.getParameter("op");
        System.out.println("--op--" + op);
//        String name = request.getParameter("name");
//        System.out.println("--name---" + name);
        //根据页面数据做判断
        if("all".equals(op)){
            listAll(request,response);
        }else if("add".equals(op)){
            addStudent(request,response);
        }else if("update".equals(op)){
            update(request,response);
        }else if("delete".equals(op)){
            delete(request,response);
        }else if("delmore".equals(op)){
            delmore(request,response);
        }else if("toupdate".equals(op)){
            toupdate(request,response);
        }
    }

    private void toupdate(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        // 封装页面传递过来的数据
        //封装页面数据
                StudentFromBean sfb = WebUtils.fillFormBean(StudentFromBean.class, request);
                System.out.println("---formbean---" + sfb.toString());
                //检测数据
                System.out.println("===validate==="  + sfb.validate());
                if(sfb.validate()){
                    //验证通过,将forbean中的数据拷贝到javabean中
                    Student s = new Student();
                    //由于时间是date类型,需要注册一个时间转换器
                    ConvertUtils.register(new DateLocaleConverter(), Date.class);
                    try {
                        BeanUtils.copyProperties(s, sfb);
                    } catch (IllegalAccessException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (InvocationTargetException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    System.out.println("---update封装了进来的数据---" + s);
                    //根据formbean对象的id来封装数据,应该是已经封装进来了
//                    s.setId(WebTools.createNewId());
                    //其次由于hobby的类型不同,所以不会拷贝数据,需要收到拷贝
                    //拿到页面的爱好数组,将数组拼接成一个字符串
                    String[] hobby = sfb.getHobby();
                    if(hobby != null && hobby.length > 0){
                        StringBuffer sf = new StringBuffer(hobby[0]);
                        for (int i = 1; i < hobby.length; i++) {
                            sf.append("," + hobby[i]);
                        }
                        s.setHobby(sf.toString());
                    }
                    //此时应该是已经封装完成了  student对象s
                    System.out.println("---封装了全部数据,准备写入数据库---" + s);
                    //调用service层完成业务逻辑,更新数据
                    
                    boolean flag = ss.update(s);
                    if(flag){
                        //说明添加成功,转向主页面,
                        //先重新查询数据库,拿取数据后在转向主页
                        listAll(request, response);
                    }else{
                        //添加失败
                        request.setAttribute("errors", "修改失败");
                        request.getRequestDispatcher(request.getContextPath() + "/update.jsp");
                    }
                }else{
                    //验证失败,踢回去,sfb对象存入request对象中,错误信息显示到页面上
                    //算是数据回显
                    request.setAttribute("user", sfb);
                    //给页面传递的
                    request.getRequestDispatcher("/update.jsp").forward(request, response);
                }
    }

    private void delmore(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        // 获取传递的ids
         String all = request.getParameter("ids");
         System.out.println("--all---" + all);
        //由于ids后面多了一个逗号,记得去掉   ----> 测试没有这一步也没有问题
        //ids = ids.substring(0, ids.length()-1) ;
         //拿到了ids,拆分后多次调用
         String [] ids = all.split(",") ;
         for (int i = 0; i < ids.length; i++) {
             System.out.println("id" +i+"---"+ ids[i]);
             if(!ss.delete(ids[i])){
                //删除失败
                    request.getSession().setAttribute("error", "删除失败");
             }
        }
         listAll(request, response);
    }

    private void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException  {
        //根据id删除对应的行
        String id = request.getParameter("id");
        System.out.println("--id--" + id);
        //拿到了页面传递过来的id,根据id来删除用户
        if(!ss.delete(id)){
            //删除失败
            request.getSession().setAttribute("error", "删除失败");
        }
        listAll(request, response);
    }

    private void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        //获取id
        String id = request.getParameter("id");
        //根据id来更新内容
        //根据id找到该用户,将该用户信息发送到更新页面上,提交到数据库
        System.out.println("--update--" + id);
        Student s = ss.findStudentById(id);
        System.out.println("--find--" + s);
        if(s != null){
            //请求重定向,必须存放到session中,为什么不使用重定向 ??? 拭目以待
            request.getSession().setAttribute("s", s);
//            response.sendRedirect(request.getContextPath() + "/update.jsp");
            request.getRequestDispatcher("/update.jsp").forward(request, response);
        }
    }

    private void addStudent(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException  {

        //封装页面数据
        StudentFromBean sfb = WebUtils.fillFormBean(StudentFromBean.class, request);
        System.out.println("---formbean---" + sfb.toString());
        //检测数据
        System.out.println("===validate==="  + sfb.validate());
        if(sfb.validate()){
            //验证通过,将forbean中的数据拷贝到javabean中
            Student s = new Student();
            //由于时间是date类型,需要注册一个时间转换器
            ConvertUtils.register(new DateLocaleConverter(), Date.class);
            try {
                BeanUtils.copyProperties(s, sfb);
            } catch (IllegalAccessException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println("---封装了一部分内容---" + s);
            //此时的s对象中没有id属性,需要通过一个类来生成id
            s.setId(WebTools.createNewId());
            //其次由于hobby的类型不同,所以不会拷贝数据,需要收到拷贝
            //拿到页面的爱好数组,将数组拼接成一个字符串
            String[] hobby = sfb.getHobby();
            if(hobby != null && hobby.length > 0){
                StringBuffer sf = new StringBuffer(hobby[0]);
                for (int i = 1; i < hobby.length; i++) {
                    sf.append("," + hobby[i]);
                }
                s.setHobby(sf.toString());
            }
            //此时应该是已经封装完成了  student对象s
            System.out.println("---封装了全部数据,准备写入数据库---" + s);
            //调用service层完成业务逻辑
            boolean flag = ss.add(s);
            if(flag){
                //说明添加成功,转向主页面,
                //先重新查询数据库,拿取数据后在转向主页
                listAll(request, response);
            }else{
                //添加失败
                request.setAttribute("errors", "添加失败");
                request.getRequestDispatcher(request.getContextPath() + "/add.jsp");
            }
        }else{
            //验证失败,踢回去,sfb对象存入request对象中,错误信息显示到页面上
            //算是数据回显
            request.setAttribute("user", sfb);
            //给页面传递的
            request.getRequestDispatcher("/add.jsp").forward(request, response);
            
        }
    }

    private void listAll(HttpServletRequest request,
            HttpServletResponse response) throws IOException {

        //显示所有的数据
        //拿到所有的数据
        /**
         * 分层思想,表现层调用业务逻辑层来完成,不需要管他们怎么实现。
         */
        List<Student> list = ss.getAllStudent();
        //将数据存放到session中
        /**
         * 为什么要放到session中而不是request中 ?
         * 这个地方如果采用请求转发,增加或改动之后,就会转发到这里来重新查询,但是当转发过来之后,又是一次。算了,自己试一次就知道了。
         * 
         * 
         * 重定向和转发有一个重要的不同:当使用转发时,JSP容器将使用一个内部的方法来调用目标页面,新的页面继续处理同一个请求,而浏览器将不会知道这个过程。
         *  与之相反,重定向方式的含义是第一个页面通知浏览器发送一个新的页面请求。因为,当你使用重定向时,浏览器中所显示的URL会变成新页面的URL,
         *  而当使用转发时,该URL会保持不变。重定向的速度比转发慢,因为浏览器还得发出一个新的请求。同时,由于重定向方式产生了一个新的请求,所以经过一次重 定向后,
         *  request内的对象将无法使用。 怎么选择是重定向还是转发呢?通常情况下转发更快,而且能保持request内的对象,所以他是第一选择。但是由于在转发之后,
         *  浏览器中URL仍然指向开始页面,此时如果重载当前页面,开始页面将会被重新调用。如果你不想看到这样的情况,则选择转发。 
         */
        
        //请求重定向,必须存放到session中
        request.getSession().setAttribute("list", list);
        response.sendRedirect(request.getContextPath() + "/list.jsp");
    }

    
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        doGet(request, response);
    }

}

dbcfg.properties

driverClass=com.mysql.jdbc.Driver
url=jdbc\:mysql\://localhost\:3306/abc
user=abc
password=defg

index.jsp  关键就是那一句

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>欢迎</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
  
  <body>
   欢迎回来 <br>
   <hr>
   <a href="${pageContext.request.contextPath }/servlet/Controller?op=all">显示全部数据</a>
   
  </body>
</html>

list.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>学生管理系统</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>
  <style type="text/css">
          #t1{
              width=900px;
          }
          #t2{
              border:1px solid gray;
              border-collapse: collapse <!--表格的边框合并成一个单一的边框 -->
              font-size:15px;
              text-align:center;
          }
          #t2 td,tr,th{
              border:1px,solid gray
          }
          #t2 tr:hover{
              background-color: ffccff;  <!-- 鼠标移动后变色 -->
          }
          
          
  </style>
  <script type="text/javascript">
      function checkAll(flag){
          //拿到所有的记录
          var ids = document.getElementsByName("ids");
          //循环设置每一个复选框
          for (var i = 0; i < ids.length; i++) {
            ids[i].checked = flag;
        }
      }
      
      function delmore(){
          //拿到所有的记录的复选框
          var ids =document.getElementsByName("ids");
          //构建id字符串,循环判断每一个复选框是否选中
          var s = "";
          for(var i = 0; i <ids.length; i++){
              if(ids[i].checked == true){
                  //拿到复选框的value
                  s += ids[i].value + ",";
              }
          }
          //数据传递到服务端进行删除
          window.location = "${pageContext.request.contextPath}/servlet/Controller?op=delmore&ids=" + s ;
      }
      
  </script>
  <body>
    学生管理系统 <br>
    <h1>学生信息</h1>
    <hr>
    <font color =red>${error }</font>
    <table id="t1" border=3>
        <tr>
            <td>
                <a href="${pageContext.request.contextPath }/add.jsp">添加</a>&nbsp;&nbsp;&nbsp;&nbsp;
                <a href="javascript:delmore()">删除</a>
            </td>
        </tr>
        <tr>
            <td>
                <table id = "t2" width="100%"> <!-- 占据单元格的100% -->
                    <tr>
                        <th><input type="checkbox" id="all" onclick="checkAll(this.checked)">全选全不选</th>
                        <th>姓名</th>
                        <th>性别</th>
                        <th>生日</th>
                        <th>电话</th>
                        <th>邮箱</th>
                        <th>爱好</th>
                        <th>类型</th>
                        <th>描述</th>
                        <th>操作</th>
                    </tr>
                    <c:choose>
                        <c:when test="${empty list}">
                            <tr>
                                <td colspan="10" align = "center">暂时没有数据</td>
                            </tr>
                        </c:when>
                        <c:otherwise>
                            <c:forEach items="${list}" var="c">
                                <tr>
                                    <td><input type="checkbox" name="ids" value="${c.id }"></td>
                                    <td>${c.name }</td>
                                    <td>${c.gender == "1" ? "" : ""}</td>
                                    <td>${c.birthday }</td>
                                    <td>${c.cellphone }</td>
                                    <td>${c.email }</td>
                                    <td>${c.hobby }</td>
                                    <td>${c.type == "vip" ? "贵宾" : "会员" }</td>
                                    <td>${c.description }</td>
                                    <td><a href="${pageContext.request.contextPath }/servlet/Controller?op=update&id=${c.id}">修改</a>&nbsp;&nbsp;&nbsp;<a href="${pageContext.request.contextPath }/servlet/Controller?op=delete&id=${c.id} ">删除</a></td>
                                </tr>
                            </c:forEach>
                        </c:otherwise>
                    </c:choose>
                </table>
            </td>
        </tr>
    </table>
    
  </body>
</html>

add.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>添加学生信息</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

    <script type="text/javascript"
            src="${pageContext.request.contextPath }/js/Birthday-Calendar.js"></script>
  </head>
  
  <body>
    <h1 align="center">添加学生信息</h1>
    <hr>
    <form action="${pageContext.request.contextPath }/servlet/Controller?op=add" method="post">
        <table align="center" border="1">
            <tr>
                <td align="right" width="40%">姓名</td>
                <td align="left"><input type="text" name="name" value="${user.name }"></td>
                <td ><font color="red">${user.errors.name}</font></td>
            </tr>
            <tr>
                <td align="right" width="40%">性别</td>
                <td align="left"><input type="radio" name="gender" value="1" checked>男<input type="radio" name="gender" value="0">女</td>
            </tr>
            <tr>
                <td align="right" width="40%">生日</td>
                <td align="left"><input type="text" name="birthday" onfocus="new Calendar().show(this)" readonly="readonly"></td>
                <!-- 这一段不明白如何添加日期 -->
            </tr>
            <tr>
                <td align="right" width="40%">电话:</td>
                <td align="left"><input type="text" name="cellphone"></td>
            </tr>
            <tr>
                <td align="right" width="40%">邮箱:</td>
                <td align="left"><input type="text" name="email"></td>
            </tr>
            <tr>
                <td align="right" width="40%">爱好</td>
                <td align="left"><input type="checkbox" name="hobby" value="骑行">骑行<input type="checkbox" name="hobby" value="游泳">游泳
                                 <input type="checkbox" name="hobby" value="看电影">看电影</td>
            </tr>
            <tr>
                <td align="right" width="40%">类型</td>
                <td align="left"><input type="radio" name="type" value="vip" checked>贵宾<input type="radio" name="type" value="common">会员</td>
            </tr>
            <tr>
                <td align="right" width="40%">描述</td>
                <td align="left"><textarea rows="5" cols="20" name="description">沉舟侧畔千帆过,病树前头万木春</textarea></td>
            </tr>
            <tr>
                <td align="center" colspan="2"><input type="submit" value="添加"></td>
            </tr>
        </table>
    </form>
  </body>
</html>

update.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fun" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>添加学生信息</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

    <script type="text/javascript"
            src="${pageContext.request.contextPath }/js/Birthday-Calendar.js"></script>
  </head>
  
  <body>
    <h1 align="center">修改学生信息</h1>
    <hr>
    <form action="${pageContext.request.contextPath }/servlet/Controller?op=toupdate" method="post">
        <table align="center" border="1">
            <tr>
                <td align="left" colspan = "2"><input type="hidden" name="id" value="${s.id}"></td>
            </tr>
            <tr>
                <td align="right" width="40%">姓名</td>
                <td align="left"><input type="text" name="name" value="${s.name }"></td>
                <td ><font color="red">${user.errors.name}</font></td>
            </tr>
            <tr>
                <td align="right" width="40%">性别</td>
                <td align="left"><input type="radio" name="gender" value="1" ${s.gender == "1" ? "checked" : ""} >男<input type="radio" name="gender" value="0" ${s.gender == "0" ? "checked" : ""} >女</td>
            </tr>
            <tr>
                <td align="right" width="40%">生日</td>
                <td align="left"><input type="text" name="birthday" onfocus="new Calendar().show(this)" readonly="readonly" value="${s.birthday}"></td>
                <!-- 这一段不明白如何添加日期 -->
            </tr>
            <tr>
                <td align="right" width="40%">电话:</td>
                <td align="left"><input type="text" name="cellphone" value="${s.cellphone }"></td>
            </tr>
            <tr>
                <td align="right" width="40%">邮箱:</td>
                <td align="left"><input type="text" name="email" value="${s.email}"></td>
            </tr>
            <tr>
                <td align="right" width="40%">爱好</td><!-- 由于hobby传递过来的是一个字符串,所以使用contains: -->
                <td align="left"><input type="checkbox" name="hobby" value="骑行" ${fun:contains(s.hobby,"骑行")?"checked":""}>骑行<input type="checkbox" name="hobby" value="游泳" ${fun:contains(s.hobby,"游泳")?"checked":""}>游泳
                                 <input type="checkbox" name="hobby" value="看电影" ${fun:contains(s.hobby,"看电影")?"checked":""}>看电影</td>
            </tr>
            <tr>
                <td align="right" width="40%">类型</td>
                <td align="left"><input type="radio" name="type" value="vip" ${s.type == "vip" ? "checked" : "" }>贵宾<input type="radio" name="type" value="common" ${s.type == "common" ? "checked" : "" }>会员</td>
            </tr>
            <tr>
                <td align="right" width="40%">描述</td>
                <td align="left"><textarea rows="5" cols="20" name="description">${s.description}</textarea></td>
            </tr>
            <tr>
                <td align="center" colspan="2"><input type="submit" value="保存"></td>
            </tr>
        </table>
    </form>
  </body>
</html>