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寫成了 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>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM