第83节:Java中的学生管理系统分页功能
分页功能一般可以做成两种,一种是物理分页,另一种是逻辑分页。这两种功能是有各自的特点的,物理分页是查询的时候,对数据库进行访问,只是查一页数据就进行返回,其特点是对内存中数据量存储不大,只是缺点就是要对数据库不断的进行访问;而对逻辑分页来说,就有所不同,它是一下子就把所有的数据全部查询出来,然后放入到内存中,访问速度快,缺点就是对内存空间不足,数据量过大。
select * from stu limit 5;
// offset 偏移前面的多少条,offset 1 跳过前面的一条
select * from stu limit 5 offset 5;
SELECT * FROM stu LIMIT 5 , 5;
SELECT * FROM stu LIMIT 5 , 2;
SELECT * FROM stu LIMIT 2 , 5;
写分页的dao模式
// index.jsp
分页显示所有学生
// StudentListPageServlet
//1. 获取需要显示的页码数
int currentPage =Integer.parseInt( request.getParameter("currentPage"));
// StudentDao
// 接口中定义的成员都是常量
// 一页显示多少条记录
int PAGE_SIZE = 5;
// 分页dao,查询当页的学生数据
List findStudentByPage(int currentPage) throws SQLException;
// StudentDaoImpl
@Override
public List findStudentByPage(int currentPage) throws SQLException {
// TODO Auto-generated method stub
QueryRunner runner = new QueryRunner(JDBCUtil02.getDataSource());
// 第一个问号,一页返回多少条记录,第二个问号,跳过前面的多少条记录
//5 0 --- 第一页 (1-1)*5
//5 5 --- 第二页 (2-1)*5
//5 10 --- 第三页
return runner.query("select * from stu limit ? offset ?", new BeanListHandler(Student.class),PAGE_SIZE , (currentPage-1)*PAGE_SIZE);
}
业务逻辑
select count(*) from stu;
业务逻辑
package com.dashucoding.servlet;
import java.io.IOException;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.dashucoding.domain.PageBean;
import com.dashucoding.service.StudentService;
import com.dashucoding.service.impl.StudentServiceImpl;
/**
* 这是用于分页显示学生列表的servlet
*/
public class StudentListPageServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
//1. 获取需要显示的页码数
int currentPage =Integer.parseInt( request.getParameter("currentPage"));
//2. 根据指定的页数,去获取该页的数据回来
//List --- list.jsp
StudentService service = new StudentServiceImpl();
PageBean pageBean= service.findStudentByPage(currentPage);
request.setAttribute("pageBean", pageBean);
//3. 跳转界面。
request.getRequestDispatcher("list_page.jsp").forward(request, response);
} catch (SQLException e) {
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
去业务逻辑去找
package com.dashucoding.service;
import java.sql.SQLException;
import java.util.List;
import com.dashucoding.domain.PageBean;
import com.dashucoding.domain.Student;
/*
* 这是学生的业务处理规范
* */
public interface StudentService {
// 分页dao,查询当页的学生数据
// 分页的很多小逻辑业务 可以做到service里面
// 业务做到service里面,做的就是bean了,当前页,总页数,显示条数, 总记录数
// 返回的是一个bean了
// 返回的是PageBean里面的所有数据了
PageBean findStudentByPage(int currentPage) throws SQLException;
// 根据姓名或性别,查询
List searchStudent(String sname, String sgender) throws SQLException;
/*
* 查询所有学生 list
*/
List findAll() throws SQLException;
void insert(Student student) throws SQLException;
// sid根据id删除学生
void delete(int sid) throws SQLException;
// 根据id查询单个学生对象
Student findStudentById(int sid) throws SQLException;
// 更新学生信息
void update(Student student) throws SQLException;
}
package com.dashucoding.service.impl;
import java.sql.SQLException;
import java.util.List;
import com.dashucoding.dao.StudentDao;
import com.dashucoding.dao.impl.StudentDaoImpl;
import com.dashucoding.domain.PageBean;
import com.dashucoding.domain.Student;
import com.dashucoding.service.StudentService;
/*
* 这是学生业务实现
* */
public class StudentServiceImpl implements StudentService {
@Override
public List findAll() throws SQLException {
StudentDao dao = new StudentDaoImpl();
return dao.findAll();
}
@Override
public void insert(Student student) throws SQLException {
// TODO Auto-generated method stub
StudentDao dao = new StudentDaoImpl();
dao.insert(student);
}
@Override
public void delete(int sid) throws SQLException {
// TODO Auto-generated method stub
StudentDao dao = new StudentDaoImpl();
dao.delete(sid);
}
@Override
public Student findStudentById(int sid) throws SQLException {
// TODO Auto-generated method stub
StudentDao dao = new StudentDaoImpl();
return dao.findStudentById(sid);
}
@Override
public void update(Student student) throws SQLException {
// TODO Auto-generated method stub
StudentDao dao = new StudentDaoImpl();
dao.update(student);
}
@Override
public List searchStudent(String sname, String sgender) throws SQLException {
// TODO Auto-generated method stub
StudentDao dao = new StudentDaoImpl();
return dao.searchStudent(sname, sgender);
}
@Override
public PageBean findStudentByPage(int currentPage) throws SQLException {
// TODO Auto-generated method stub
// 封装分页的该页的数据
PageBean pageBean = new PageBean();
int pageSize = StudentDao.PAGE_SIZE;
// 设置当前页
pageBean.setCurrentPage(currentPage);
// 每条记录
pageBean.setPageSize(pageSize);
StudentDao dao = new StudentDaoImpl();
List list = dao.findStudentByPage(currentPage);
pageBean.setList(list);
// 总记录数,总页数
int count = dao.findCount();
pageBean.setTotalSize(count);
// 总页数
pageBean.setTotalPage(count % pageSize == 0 ? count / pageSize : (count / pageSize) + 1);
return null;
}
}
// 封装的数据
package com.dashucoding.domain;
import java.util.List;
// 一个用于封装了分页的数据
// 有: 当前学生集合数据,总的记录数,总的页数,当前页,每页的显示记录数
public class PageBean {
private int currentPage; // 当前页
private int totalPage;// 总页数
private int pageSize;// 每页的记录数,每页要显示多少记录
private int totalSize; // 总的记录数
private List list; // 当前页的学生集合
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalSize() {
return totalSize;
}
public void setTotalSize(int totalSize) {
this.totalSize = totalSize;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
}
最后就靠显示页面逻辑
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
学生列表页面
我的源码
package com.dashucoding.dao;
import java.sql.SQLException;
import java.util.List;
import com.dashucoding.domain.Student;
/*
* 这是针对学生表的数据访问
*
* */
public interface StudentDao {
// 接口中定义的成员都是常量
// 一页显示多少条记录
int PAGE_SIZE = 5;
// 分页dao,查询当页的学生数据
List findStudentByPage(int currentPage) throws SQLException;
// 根据姓名或性别,查询
List searchStudent(String sname, String sgender) throws SQLException;
/*
* 查询所有学生 list
*/
List findAll() throws SQLException;
void insert(Student student) throws SQLException;
// sid根据id删除学生
void delete(int sid) throws SQLException;
// 根据id查询单个学生对象
Student findStudentById(int sid) throws SQLException;
// 更新学生信息
void update(Student student) throws SQLException;
// 查询总的学生记录数
int findCount() throws SQLException;
}
package com.dashucoding.dao.impl;
import java.sql.Connection;
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.dashucoding.dao.StudentDao;
import com.dashucoding.domain.Student;
import com.dashucoding.util.JDBCUtil02;
import com.dashucoding.util.TextUtils;
/*
*这是StudentDao的实现,针对前面定义的规范,做出具体的实现
* */
public class StudentDaoImpl implements StudentDao {
/*
* 查询所有学生
*/
@Override
public List findAll() throws SQLException {
QueryRunner runner = new QueryRunner(JDBCUtil02.getDataSource());
return runner.query("select * from stu", new BeanListHandler(Student.class));
}
@Override
public void insert(Student student) throws SQLException {
// TODO Auto-generated method stub
QueryRunner runner = new QueryRunner(JDBCUtil02.getDataSource());
runner.update("insert into stu values(null, ?,?,?,?,?,?)",
student.getSname(),
student.getGender(),
student.getPhone(),
student.getBirthday(),
student.getHobby(),
student.getInfo()
);
}
@Override
public void delete(int sid) throws SQLException {
// TODO Auto-generated method stub
QueryRunner runner = new QueryRunner(JDBCUtil02.getDataSource());
runner.update("delete from stu where sid=?", sid);
}
@Override
public Student findStudentById(int sid) throws SQLException {
// TODO Auto-generated method stub
QueryRunner runner = new QueryRunner(JDBCUtil02.getDataSource());
return runner.query("select * from stu where sid = ?", new BeanHandler(Student.class), sid);
}
@Override
public void update(Student student) throws SQLException {
// TODO Auto-generated method stub
QueryRunner runner = new QueryRunner(JDBCUtil02.getDataSource());
runner.update("update stu set sname=?, gender=?, phone=?, birthday=?, hobby=?, info=? where sid=?",
student.getSname(),
student.getGender(),
student.getPhone(),
student.getBirthday(),
student.getHobby(),
student.getInfo(),
student.getSid());
}
// 模糊查询
@Override
public List searchStudent(String sname, String sgender) throws SQLException {
// TODO Auto-generated method stub
/*System.out.println(sname + sgender);*/
QueryRunner runner = new QueryRunner(JDBCUtil02.getDataSource());
/*
* String sql = "select * from stu where sname=? or sgender=?";
* select * from stu where sname like ?;
* select * from stu where gender = ?;
* select * from stu where sname like ? and gender = ?;
* 如果两个都没有就查询所有
* sql = "select * from stu"
* if(姓名){
* sql = sql + "where sname like ?";
* }
* if(性别){
* sql = sql + "where gender = ?";
* }
*
* String sql = "select * from stu where 1=1";
* if(姓名){
* sql = sql + " and sname like ? ";
* }
* if(性别){
* sql = sql + " and gender = ? ";
* }
* */
String sql = "select * from stu where 1=1";
List list = new ArrayList();
if(!TextUtils.isEmpty(sname)) {
sql = sql + " and sname like ? ";
list.add("%"+sname+"%");
}
if(!TextUtils.isEmpty(sgender)) {
sql = sql + " and gender = ? ";
list.add(sgender);
}
/*list.toArray()*/
return runner.query(sql, new BeanListHandler(Student.class),list.toArray());
}
@Override
public List findStudentByPage(int currentPage) throws SQLException {
// TODO Auto-generated method stub
QueryRunner runner = new QueryRunner(JDBCUtil02.getDataSource());
// 第一个问号,一页返回多少条记录,第二个问号,跳过前面的多少条记录
//5 0 --- 第一页 (1-1)*5
//5 5 --- 第二页 (2-1)*5
//5 10 --- 第三页
return runner.query("select * from stu limit ? offset ?", new BeanListHandler(Student.class),PAGE_SIZE , (currentPage-1)*PAGE_SIZE);
}
// 查询总的记录数
@Override
public int findCount() throws SQLException {
// TODO Auto-generated method stub
QueryRunner runner = new QueryRunner(JDBCUtil02.getDataSource());
// 用于处理平均值,总的个数
Long result = (Long) runner.query("select count(*) from stu", new ScalarHandler());
return result.intValue();
}
}
package com.dashucoding.domain;
import java.util.List;
// 一个用于封装了分页的数据
// 有: 当前学生集合数据,总的记录数,总的页数,当前页,每页的显示记录数
public class PageBean {
private int currentPage; // 当前页
private int totalPage;// 总页数
private int pageSize;// 每页的记录数,每页要显示多少记录
private int totalSize; // 总的记录数
private List list; // 当前页的学生集合
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalSize() {
return totalSize;
}
public void setTotalSize(int totalSize) {
this.totalSize = totalSize;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
}
package com.dashucoding.domain;
import java.util.Date;
/*
* 这是学生封装的对象bean
*
* */
public class Student {
private int sid;
private String sname;
private String gender;
private String phone;
private String hobby;
private String info;
private Date birthday;
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(int sid, String sname, String gender, String phone, String hobby, String info, Date birthday) {
super();
this.sid = sid;
this.sname = sname;
this.gender = gender;
this.phone = phone;
this.hobby = hobby;
this.info = info;
this.birthday = birthday;
}
public Student(String sname, String gender, String phone, String hobby, String info, Date birthday) {
super();
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 Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
return "Student [sid=" + sid + ", sname=" + sname + ", gender=" + gender + ", phone=" + phone + ", hobby="
+ hobby + ", info=" + info + ", birthday=" + birthday + "]";
}
}
package com.dashucoding.service;
import java.sql.SQLException;
import java.util.List;
import com.dashucoding.domain.PageBean;
import com.dashucoding.domain.Student;
/*
* 这是学生的业务处理规范
* */
public interface StudentService {
// 分页dao,查询当页的学生数据
// 分页的很多小逻辑业务 可以做到service里面
// 业务做到service里面,做的就是bean了,当前页,总页数,显示条数, 总记录数
// 返回的是一个bean了
// 返回的是PageBean里面的所有数据了
PageBean findStudentByPage(int currentPage) throws SQLException;
// 根据姓名或性别,查询
List searchStudent(String sname, String sgender) throws SQLException;
/*
* 查询所有学生 list
*/
List findAll() throws SQLException;
void insert(Student student) throws SQLException;
// sid根据id删除学生
void delete(int sid) throws SQLException;
// 根据id查询单个学生对象
Student findStudentById(int sid) throws SQLException;
// 更新学生信息
void update(Student student) throws SQLException;
}
package com.dashucoding.service.impl;
import java.sql.SQLException;
import java.util.List;
import com.dashucoding.dao.StudentDao;
import com.dashucoding.dao.impl.StudentDaoImpl;
import com.dashucoding.domain.PageBean;
import com.dashucoding.domain.Student;
import com.dashucoding.service.StudentService;
/*
* 这是学生业务实现
* */
public class StudentServiceImpl implements StudentService {
@Override
public List findAll() throws SQLException {
StudentDao dao = new StudentDaoImpl();
return dao.findAll();
}
@Override
public void insert(Student student) throws SQLException {
// TODO Auto-generated method stub
StudentDao dao = new StudentDaoImpl();
dao.insert(student);
}
@Override
public void delete(int sid) throws SQLException {
// TODO Auto-generated method stub
StudentDao dao = new StudentDaoImpl();
dao.delete(sid);
}
@Override
public Student findStudentById(int sid) throws SQLException {
// TODO Auto-generated method stub
StudentDao dao = new StudentDaoImpl();
return dao.findStudentById(sid);
}
@Override
public void update(Student student) throws SQLException {
// TODO Auto-generated method stub
StudentDao dao = new StudentDaoImpl();
dao.update(student);
}
@Override
public List searchStudent(String sname, String sgender) throws SQLException {
// TODO Auto-generated method stub
StudentDao dao = new StudentDaoImpl();
return dao.searchStudent(sname, sgender);
}
@Override
public PageBean findStudentByPage(int currentPage) throws SQLException {
// TODO Auto-generated method stub
// 封装分页的该页的数据
PageBean pageBean = new PageBean();
int pageSize = StudentDao.PAGE_SIZE;
// 设置当前页
pageBean.setCurrentPage(currentPage);
// 每条记录
pageBean.setPageSize(pageSize);
StudentDao dao = new StudentDaoImpl();
List list = dao.findStudentByPage(currentPage);
pageBean.setList(list);
// 总记录数,总页数
int count = dao.findCount();
pageBean.setTotalSize(count);
// 总页数
pageBean.setTotalPage(count % pageSize == 0 ? count / pageSize : (count / pageSize) + 1);
return null;
}
}
package com.dashucoding.servlet;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.dashucoding.domain.Student;
import com.dashucoding.service.StudentService;
import com.dashucoding.service.impl.StudentServiceImpl;
/**
* 用于处理学生的添加请求
*/
public class AddServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
try {
// 1. 获取客户端提交上来的数据
String sname = request.getParameter("sname");
String gender = request.getParameter("gender");
String phone = request.getParameter("phone");
String birthday = request.getParameter("birthday");
String info = request.getParameter("info");
// String hobby = request.getParameter("hobby");//hobby : 游泳,写字, 足球。
String[] h = request.getParameterValues("hobby");
String hobby = Arrays.toString(h);
hobby = hobby.substring(1, hobby.length() - 1);
// 2. 添加到数据库
// string -- date
Date date = new SimpleDateFormat("yyyy-MM-dd").parse(birthday);
Student student = new Student(sname, gender, phone, hobby, info, date);
StudentService service = new StudentServiceImpl();
service.insert(student);
// 3. 跳转到列表页
request.getRequestDispatcher("StudentListServlet").forward(request, response);
} catch (Exception e) {
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
package com.dashucoding.servlet;
import java.io.IOException;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.dashucoding.service.StudentService;
import com.dashucoding.service.impl.StudentServiceImpl;
/**
* 用于处理删除学生
*/
public class DeleteServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
int sid = Integer.parseInt(request.getParameter("sid"));
// System.out.println("sid="+sid);
// 执行删除
StudentService service = new StudentServiceImpl();
service.delete(sid);
// 跳转到列表页
request.getRequestDispatcher("StudentListServlet").forward(request, response);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
package com.dashucoding.servlet;
import java.io.IOException;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.dashucoding.domain.Student;
import com.dashucoding.service.StudentService;
import com.dashucoding.service.impl.StudentServiceImpl;
/**
* 处理单个学生的更新,查询学生的信息,跳转到更新的页面
*/
public class EditServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
// 接收id
int sid = Integer.parseInt(request.getParameter("sid"));
// 查询学生数据
StudentService service = new StudentServiceImpl();
Student stu = service.findStudentById(sid);
// 显示数据
// 存储数据
request.setAttribute("stu", stu);
// 跳转
request.getRequestDispatcher("edit.jsp").forward(request, response);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
package com.dashucoding.servlet;
import java.io.IOException;
import java.sql.SQLException;
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.dashucoding.domain.Student;
import com.dashucoding.service.StudentService;
import com.dashucoding.service.impl.StudentServiceImpl;
/**
* Servlet implementation class SearchStudentServlet
*/
public class SearchStudentServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
try {
// 取到了要查询的关键数据
String sname = request.getParameter("sname");
String sgender = request.getParameter("sgender");
// 找service查询
StudentService service = new StudentServiceImpl();
List list = service.searchStudent(sname, sgender);
/*for(Student student : list) {
System.out.println("stu=" + student);
}*/
request.setAttribute("list", list);
// 跳转界面
request.getRequestDispatcher("list.jsp").forward(request, response);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
package com.dashucoding.servlet;
import java.io.IOException;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.dashucoding.domain.PageBean;
import com.dashucoding.service.StudentService;
import com.dashucoding.service.impl.StudentServiceImpl;
/**
* 这是用于分页显示学生列表的servlet
*/
public class StudentListPageServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
//1. 获取需要显示的页码数
int currentPage =Integer.parseInt( request.getParameter("currentPage"));
//2. 根据指定的页数,去获取该页的数据回来
//List --- list.jsp
StudentService service = new StudentServiceImpl();
PageBean pageBean= service.findStudentByPage(currentPage);
request.setAttribute("pageBean", pageBean);
//3. 跳转界面。
request.getRequestDispatcher("list_page.jsp").forward(request, response);
} catch (SQLException e) {
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
package com.dashucoding.servlet;
import java.io.IOException;
import java.sql.SQLException;
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.dashucoding.dao.StudentDao;
import com.dashucoding.dao.impl.StudentDaoImpl;
import com.dashucoding.domain.Student;
import com.dashucoding.service.StudentService;
import com.dashucoding.service.impl.StudentServiceImpl;
public class StudentListServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
// 查询所有的学生
StudentService service = new StudentServiceImpl();
List list = service.findAll();
// 把数据存储到作用域中
request.setAttribute("list", list);
// 跳转页面
request.getRequestDispatcher("list.jsp").forward(request,response);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
package com.dashucoding.servlet;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.dashucoding.domain.Student;
import com.dashucoding.service.StudentService;
import com.dashucoding.service.impl.StudentServiceImpl;
/**
* Servlet implementation class UpdateServlet
*/
public class UpdateServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
try {
// 1. 获取客户端提交上来的数据
int sid = Integer.parseInt(request.getParameter("sid"));
String sname = request.getParameter("sname");
String gender = request.getParameter("gender");
String phone = request.getParameter("phone");
String birthday = request.getParameter("birthday");
String info = request.getParameter("info");
// String hobby = request.getParameter("hobby");
String[] h = request.getParameterValues("hobby");
String hobby = Arrays.toString(h);
hobby = hobby.substring(1, hobby.length() - 1);
// 2. 添加到数据库
Date date = new SimpleDateFormat("yyyy-MM-dd").parse(birthday);
Student student = new Student(sid, sname, gender, phone, hobby, info, date);
// 2. 更新数据库数据
StudentService service = new StudentServiceImpl();
service.update(student);
// 3. 跳转界面
request.getRequestDispatcher("StudentListServlet").forward(request, response);
} catch (Exception e) {
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
package com.dashucoding.util;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import javax.sql.DataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class JDBCUtil02 {
static ComboPooledDataSource dataSource = null;
static {
dataSource = new ComboPooledDataSource();
}
public static DataSource getDataSource() {
return dataSource;
}
/**
* 获取连接对象
* @return
* @throws SQLException
*/
public static Connection getConn() throws SQLException{
return dataSource.getConnection();
}
/**
* 释放资源
* @param conn
* @param st
* @param rs
*/
public static void release(Connection conn , Statement st , ResultSet rs){
closeRs(rs);
closeSt(st);
closeConn(conn);
}
public static void release(Connection conn , Statement st){
closeSt(st);
closeConn(conn);
}
private static void closeRs(ResultSet rs){
try {
if(rs != null){
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
rs = null;
}
}
private static void closeSt(Statement st){
try {
if(st != null){
st.close();
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
st = null;
}
}
private static void closeConn(Connection conn){
try {
if(conn != null){
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
conn = null;
}
}
}
package com.dashucoding.util;
public class TextUtils {
/**
* 判断某一个字符串是否为空。
*
* @param s
* @return
*/
public static boolean isEmpty(CharSequence s) {
return s == null || s.length() == 0;
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
添加学生页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
更新学生页面
更新学生页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
首页
显示所有学生列表
分页显示所有学生
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
学生列表页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
学生列表页面
package com.dashucoding.service.impl;
import java.sql.SQLException;
import java.util.List;
import com.dashucoding.dao.StudentDao;
import com.dashucoding.dao.impl.StudentDaoImpl;
import com.dashucoding.domain.PageBean;
import com.dashucoding.domain.Student;
import com.dashucoding.service.StudentService;
/*
* 这是学生业务实现
* */
public class StudentServiceImpl implements StudentService {
@Override
public List findAll() throws SQLException {
StudentDao dao = new StudentDaoImpl();
return dao.findAll();
}
@Override
public void insert(Student student) throws SQLException {
// TODO Auto-generated method stub
StudentDao dao = new StudentDaoImpl();
dao.insert(student);
}
@Override
public void delete(int sid) throws SQLException {
// TODO Auto-generated method stub
StudentDao dao = new StudentDaoImpl();
dao.delete(sid);
}
@Override
public Student findStudentById(int sid) throws SQLException {
// TODO Auto-generated method stub
StudentDao dao = new StudentDaoImpl();
return dao.findStudentById(sid);
}
@Override
public void update(Student student) throws SQLException {
// TODO Auto-generated method stub
StudentDao dao = new StudentDaoImpl();
dao.update(student);
}
@Override
public List searchStudent(String sname, String sgender) throws SQLException {
// TODO Auto-generated method stub
StudentDao dao = new StudentDaoImpl();
return dao.searchStudent(sname, sgender);
}
@Override
public PageBean findStudentByPage(int currentPage) throws SQLException {
// 封装分页的该页数据
PageBean pageBean = new PageBean();
int pageSize = StudentDao.PAGE_SIZE;
pageBean.setCurrentPage(currentPage);
// 设置当前页
pageBean.setPageSize(pageSize);
// 设置每页显示多少记录
StudentDao dao = new StudentDaoImpl();
List list = dao.findStudentByPage(currentPage);
pageBean.setList(list);
// 设置这一页的学生数据
// 总的记录数, 总的页数。
int count = dao.findCount();
pageBean.setTotalSize(count);
// 设置总的记录数
pageBean.setTotalPage(count % pageSize == 0 ? count / pageSize : (count / pageSize) + 1); // 总页数
return pageBean;
}
}
结言
好了,欢迎在留言区留言,与大家分享你的经验和心得。
感谢你学习今天的内容,如果你觉得这篇文章对你有帮助的话,也欢迎把它分享给更多的朋友,感谢。
达叔小生:往后余生,唯独有你
You and me, we are family !
90后帅气小伙,良好的开发习惯;独立思考的能力;主动并且善于沟通
博客: 达叔小生
https://www.jianshu.com/u/c785ece603d1
结语
- 下面我将继续对 其他知识 深入讲解 ,有兴趣可以继续关注
- 小礼物走一走 or 点赞