源码: https://download.csdn.net/download/aihiao/14974520
1. 新建一个名为StudentManager的Web项目
2. 考入相关jar
3. c3p0-config.xml配置
4. 数据库连接池JDBCUtil.java工具类
package com.lywgames.util;import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.sql.DataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;/*** 使用数据库连接池的获取的数据库连接, 我们不用做关闭操作, 数据库连接池自行管理*/
public class JDBCUtil {private static DataSource dataSource = null;static {dataSource = new ComboPooledDataSource();}public static DataSource getDataSource() {return dataSource;}/*** 获取连接对象*/public static Connection getConn(){try {return dataSource.getConnection();} catch (SQLException e) {e.printStackTrace();}return null;}/*** 释放资源* @param st* @param rs*/public static void release(Statement st, ResultSet rs){closeRs(rs);closeSt(st);}public static void release(Statement st){closeSt(st);}public static void closeRs(ResultSet rs){try {if(rs != null){rs.close();}} catch (SQLException e) {e.printStackTrace();}finally{rs = null;}}public static void closeSt(Statement st){try {if(st != null){st.close();}} catch (SQLException e) {e.printStackTrace();}finally{st = null;}}}
5. 学生Student.java实体类
package com.lywgames.domain;import java.sql.Timestamp;/*** 这是学封装的对象 bean*/
public class Student {private int sid;private String sname;private String gender;private String phone;private String hobby;private String info;private Timestamp birthday;public Student() {}public Student(String sname, String gender, String phone, String hobby, String info, Timestamp birthday) {this.sname = sname;this.gender = gender;this.phone = phone;this.hobby = hobby;this.info = info;this.birthday = birthday;}public Student(int sid, String sname, String gender, String phone, String hobby, String info, Timestamp birthday) {super();this.sid = sid;this.sname = sname;this.gender = gender;this.phone = phone;this.hobby = hobby;this.info = info;this.birthday = birthday;}public int getSid() {return sid;}public void setSid(int sid) {this.sid = sid;}public String getSname() {return sname;}public void setSname(String sname) {this.sname = sname;}public String getGender() {return gender;}public void setGender(String gender) {this.gender = gender;}public String getPhone() {return phone;}public void setPhone(String phone) {this.phone = phone;}public String getHobby() {return hobby;}public void setHobby(String hobby) {this.hobby = hobby;}public String getInfo() {return info;}public void setInfo(String info) {this.info = info;}public Timestamp getBirthday() {return birthday;}public void setBirthday(Timestamp birthday) {this.birthday = birthday;}@Overridepublic String toString() {return "Student [sid=" + sid + ", sname=" + sname + ", gender=" + gender + ", phone=" + phone + ", hobby="+ hobby + ", info=" + info + ", birthday=" + birthday + "]";}}
6. 分页PageBean.java实体类
package com.lywgames.domain;import java.util.List;/*** 这是一个用于封装了分页的数据。* 里面包含:* 该页的学生集合数据* 总的记录数* 总的页数* 当前页* 每页显示的记录数* @param
public class PageBean
}
7. 数据库操作StudentDao.java接口
package com.lywgames.dao;import java.sql.SQLException;
import java.util.List;
import com.lywgames.domain.Student;/*** 这是针对学生表的数据访问 */
public interface StudentDao {/*** 查询所有学生* @return List
}
8. 数据库操作StudentDaoImpl.java实现类
package com.lywgames.dao.impl;import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import com.lywgames.dao.StudentDao;
import com.lywgames.domain.Student;
import com.lywgames.util.JDBCUtil;/*** 这是StudentDao的实现。 针对前面定义的规范, 做出具体的实现。*/
public class StudentDaoImpl implements StudentDao {/*** 查询所有学生*/@Overridepublic List
9. 逻辑操作StudentService.java接口
package com.lywgames.service;import java.sql.SQLException;
import java.util.List;
import com.lywgames.domain.PageBean;
import com.lywgames.domain.Student;/*** 这是学生的业务处理规范*/
public interface StudentService {int DEFAULT_PAGE_SIZE = 5; // 默认一页显示多少条记录/*** 查询所有学生* @return List
}
10. 逻辑操作StudentServiceImpl.java实现类
package com.lywgames.service.impl;import java.sql.SQLException;
import java.util.List;
import com.lywgames.dao.StudentDao;
import com.lywgames.dao.impl.StudentDaoImpl;
import com.lywgames.domain.PageBean;
import com.lywgames.domain.Student;
import com.lywgames.service.StudentService;/*** 这是学生业务实现*/
public class StudentServiceImpl implements StudentService {private StudentDao studentDao = new StudentDaoImpl();@Overridepublic List
11. 查找所有学生StudentListServlet.java
package com.lywgames.servlet;import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.lywgames.domain.Student;
import com.lywgames.service.StudentService;
import com.lywgames.service.impl.StudentServiceImpl;/*** 查找所有学生*/
public class StudentListServlet extends HttpServlet {private static final long serialVersionUID = 1L;@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {StudentService service = new StudentServiceImpl();List
}
12. 分页查找学生StudentListPageServlet.java
package com.lywgames.servlet;import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.lywgames.domain.PageBean;
import com.lywgames.domain.Student;
import com.lywgames.service.StudentService;
import com.lywgames.service.impl.StudentServiceImpl;/*** 分页查找学生*/
public class StudentListPageServlet extends HttpServlet {private static final long serialVersionUID = 1L;@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {int currentPage = Integer.valueOf(req.getParameter("currentPage")).intValue() ;StudentService service = new StudentServiceImpl();PageBean
}
13. 添加学生AddServlet.java
package com.lywgames.servlet;import java.io.IOException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.lywgames.domain.Student;
import com.lywgames.service.StudentService;
import com.lywgames.service.impl.StudentServiceImpl;/*** 添加学生*/
public class AddServlet extends HttpServlet {private static final long serialVersionUID = 1L;@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {req.setCharacterEncoding("utf-8");String sname = req.getParameter("sname");String gender = req.getParameter("gender");String phone = req.getParameter("phone");String info = req.getParameter("info");long time = System.currentTimeMillis();try {time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(req.getParameter("birthday")).getTime();} catch (ParseException e) {e.printStackTrace();}Timestamp birthday = new Timestamp(time);String[] hobbys = req.getParameterValues("hobby");StringBuffer sb = new StringBuffer();for(int i = 0; i
14. 编辑学生信息EditServlet.java
package com.lywgames.servlet;import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.lywgames.domain.Student;
import com.lywgames.service.StudentService;
import com.lywgames.service.impl.StudentServiceImpl;/*** 编辑学生信息*/
public class EditServlet extends HttpServlet {private static final long serialVersionUID = 1L;@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {req.setCharacterEncoding("utf-8");String sid = req.getParameter("sid");StudentService service = new StudentServiceImpl();Student student = service.findStudentById(Integer.parseInt(sid));if(student != null) {req.setAttribute("student", student);req.getRequestDispatcher("edit.jsp").forward(req, resp);}}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req, resp);}
}
15. 更新学生信息UpdateServlet.java
package com.lywgames.servlet;import java.io.IOException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.lywgames.domain.Student;
import com.lywgames.service.StudentService;
import com.lywgames.service.impl.StudentServiceImpl;/*** 更新学生信息*/
public class UpdateServlet extends HttpServlet {private static final long serialVersionUID = 1L;@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {req.setCharacterEncoding("utf-8");int sid = Integer.parseInt(req.getParameter("sid"));String sname = req.getParameter("sname");String gender = req.getParameter("gender");String phone = req.getParameter("phone");String info = req.getParameter("info");long time = System.currentTimeMillis();try {time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(req.getParameter("birthday")).getTime();} catch (ParseException e) {e.printStackTrace();}Timestamp birthday = new Timestamp(time);String[] hobbys = req.getParameterValues("hobby");StringBuffer sb = new StringBuffer();for(int i = 0; i
16. 删除学生DeleteServlet.java
package com.lywgames.servlet;import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.lywgames.service.StudentService;
import com.lywgames.service.impl.StudentServiceImpl;/*** 删除学生*/
public class DeleteServlet extends HttpServlet {private static final long serialVersionUID = 1L;@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {req.setCharacterEncoding("utf-8");String sid = req.getParameter("sid");StudentService service = new StudentServiceImpl();int result = service.delete(Integer.parseInt(sid));if(result == 1) {req.getRequestDispatcher("StudentListServlet.action").forward(req, resp);}}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req, resp);}
}
17. 查找学生SearchStudentServlet.java
package com.lywgames.servlet;import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.lywgames.domain.Student;
import com.lywgames.service.StudentService;
import com.lywgames.service.impl.StudentServiceImpl;/*** 查找学生*/
public class SearchStudentServlet extends HttpServlet {private static final long serialVersionUID = 1L;@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {req.setCharacterEncoding("utf-8");String sname = req.getParameter("sname");String gender = req.getParameter("gender");StudentService service = new StudentServiceImpl();List
}
18. web.xml配置