1. 程式人生 > 實用技巧 >JDBC-簡單的學生管理系統-增刪改查

JDBC-簡單的學生管理系統-增刪改查

結構:三層架構--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寫成了 FormBean

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>
                <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><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>