第一步:
首先需要到如各部分岁对应的包,例如数据库驱动的包,实体类的包,c3p0的包,以及c3p0的配置文件
创建数据库,创建数据库所对应的一个实体类,例如:student类,里边包含私有的属性,及set和get方法
第二步:
创建另一个关于分页的实体类,里边只包含5项,如下可见
package com.qf.javabean;
import java.util.List;
public class PagStudent {
private int currentpage;//当前页
private int pagsize;//每页的数据量
private int totalpage;//总页数
private int totalcount;//总数据量
private List list;
public synchronized int getCurrentpage() {
return currentpage;
}
public synchronized void setCurrentpage(int currentpage) {
this.currentpage = currentpage;
}
public synchronized int getPagsize() {
return pagsize;
}
public synchronized void setPagsize(int pagsize) {
this.pagsize = pagsize;
}
public synchronized int getTotalpage() {
return (int)Math.ceil(totalcount*1.0/pagsize);
}
//记住需要删除一个方法,是算出来的,不是被设置的
public synchronized int getTotalcount() {
return totalcount;
}
public synchronized void setTotalcount(int totalcount) {
this.totalcount = totalcount;
}
public synchronized List getList() {
return list;
}
public synchronized void setList(List list) {
this.list = list;
}
public PagStudent(int currentpage, int pagsize, int totalcount, List list) {
super();
this.currentpage = currentpage;
this.pagsize = pagsize;
this.totalcount = totalcount;
this.list = list;
}
public PagStudent() {
super();
}
}
第三步:创建JSP页面
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
My JSP 'StudentPaging.jsp' starting page学员信息展示平台 | ||||||||
---|---|---|---|---|---|---|---|---|
学员编号 | 姓名 | 性别 | 年龄 | 密码 | QQ号 | 手机号 | 专业 | 班级 |
${it.id } | ${it.name } | ${it.sex } | ${it.age } | ${it.password } | ${it.qq } | ${it.phone } | ${it.major } | ${it.grade } |
page.totalpage?page.totalpage:(i+8) }" step="1"> --%>
${i }
当前${page.currentpage }/总共${page.totalpage }
第四步:创建servlet,里边包含(获取数据,调用逻辑,转发或重定向)
package com.qf.web;
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.qf.javabean.Student;
import com.qf.service.StudentService;
public class PagStudent extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
int currentpage = Integer.parseInt(request.getParameter("currentpage"));
// 当第一次访问的时候应该从请求中传递一个页数
// System.out.println(currentpage+"获取了数据");
int pagesize = 5;// 控制每页显示的数据量
try {
com.qf.javabean.PagStudent pagStudent = new StudentService().findPage(currentpage, pagesize);
request.getSession().setAttribute("page", pagStudent);
System.out.println("我又回到了这个界面");
System.out.println(pagStudent.toString());
List list = pagStudent.getList();
for (Student student : list) {
System.out.println(student);
}
System.out.println(pagStudent.getPagsize());
System.out.println(pagStudent.getList());
response.sendRedirect(request.getContextPath() + "/StudentPaging.jsp");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
request.getSession().setAttribute("msg", "分页展示数据错误");
response.sendRedirect(request.getContextPath() + "/msg.jsp");
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
第五步:创建一个Servlet包,里边包含两个方法
package com.qf.service;
import java.sql.SQLException;
import java.util.List;
import com.qf.dao.PagDao;
import com.qf.javabean.PagStudent;
import com.qf.javabean.Student;
public class StudentService {
public com.qf.javabean.PagStudent findPage(int currentpage, int pagesize) throws SQLException {
PagDao dao= new PagDao();
int totalcount= dao.findcount();//调用两个方法
List list=dao.findListPage(currentpage,pagesize);//
return new PagStudent(currentpage,pagesize,totalcount,list);
}
}
第六步:连接数据库的Dao,里边执行SQL语句
package com.qf.dao;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import com.qf.javabean.Student;
import com.qf.utils.DataSourceUtiles;
public class PagDao {
public int findcount() throws SQLException {
// TODO Auto-generated method stub
QueryRunner runner= new QueryRunner();
String sql = "select count(*) from student;";
Connection conn = DataSourceUtiles.getConnection();
Object query = runner.query(conn,sql,new ScalarHandler());
//DataSourceUtiles.close(conn);
System.out.println("数据库可以走到聚合函数");
return ((Long)query).intValue();//此处的query为long型,转化
}
public List findListPage(int currentpage, int pagesize) throws SQLException {
// TODO Auto-generated method stub
QueryRunner runner= new QueryRunner();
String sql="select *from student limit ?,?;";
Connection connection = DataSourceUtiles.getConnection();
List list = runner.query(connection, sql, new BeanListHandler(Student.class), (currentpage-1)*pagesize,pagesize);
System.out.println("这一个数据也可以走到的");
for (Student student : list) {
System.out.println(student);
}
//DataSourceUtiles.close(connection);
return list;
}
}