JSP界面中,实现翻页。实现翻页的关键在于对于数据库的操纵,使用sql语句(以mysql为例):
String sql = "select * from product limit "+(pageNo*4-4)+",4";pageNo即为你的页数,如pageNo=1时,即查找数据表product中序列为0,1,2,3的记录,
pageNo=2,查找4,5,6,7的记录。
String sql1 = "select count(*) from product";查找product中所有记录总数。
在这里贴下对数据操纵代码:
1.pageServlet
package tdw.controller;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
import tdw.beans.*;
import tdw.dao.PageImple;
public class pageServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession();
int count = 0;
int pageNo = 1;
try{
pageNo = Integer.parseInt(request.getParameter("pageNo"));
}catch(Exception e){}
if(pageNo<1){
pageNo = 1;
}
session.setAttribute("pageNo", pageNo);
List<Product> list = new ArrayList<Product>();
PageImple pageImple = new PageImple();
list = pageImple.page(pageNo);
count = pageImple.getCount();
session.setAttribute("pageList", list);
session.setAttribute("count", count);
response.sendRedirect("/SecondaryMarket/page.jsp");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
2.PageImple:
package tdw.dao;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.util.ArrayList;
import tdw.beans.Product;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
public class PageImple {
Statement st = null;
Statement st1 = null;
int count = 0;
ArrayList<Product> list = new ArrayList<Product>();
public ArrayList<Product> page(int pageNo){
Connection conn = null;
try {
list = new ArrayList<Product>();
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/xxx", "xxx", "xxx");//更改自己的密码
st = (Statement) conn.createStatement();
String sql = "select * from product limit "+(pageNo*4-4)+",4";
ResultSet rs = st.executeQuery(sql);
while(rs.next()){
Product product = null;
product = new Product();
int pID = rs.getInt("pID");
product.setpID(pID);
float pprice = rs.getFloat("pprice");
product.setPprice(pprice);
String pinstrution = rs.getString("pinstrution");
product.setPinstrution(pinstrution);
String pusername = rs.getString("pusername");
product.setPusername(pusername);
String imagepath = rs.getString("imagepath");
product.setImagepath(imagepath);
int edornot = rs.getInt("edornot");
product.setEdornot(edornot);
String pname = rs.getString("pname");
product.setPname(pname);
String pcatagory = rs.getString("pcatagory");
product.setPcatagory(pcatagory);
list.add(product);
}
}catch(Exception e){}
return list;
}
public int getCount(){
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/tdw", "root", "123");
st = (Statement) conn.createStatement();
String sql = "select count(*) from product";
ResultSet rs = st.executeQuery(sql);
while(rs.next()){
count = rs.getInt(1);
}
}catch(Exception e){}
return count;
}
}
3.product类
package tdw.beans;
public class Product {
private int pID;
private String pcatagory;
private float pprice;
private String pinstrution;
private String pusername;
private String imagepath;
private int edornot;
private String pname;
private int pnum;
public int getPnum() {
return pnum;
}
public void setPnum(int pnum) {
this.pnum = pnum;
}
public String getPusername() {
return pusername;
}
public void setPusername(String pusername) {
this.pusername = pusername;
}
public float getPprice() {
return pprice;
}
public void setPprice(float pprice) {
this.pprice = pprice;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public int getpID() {
return pID;
}
public void setpID(int pID) {
this.pID = pID;
}
public String getPcatagory() {
return pcatagory;
}
public void setPcatagory(String pcatagory) {
this.pcatagory = pcatagory;
}
public String getPinstrution() {
return pinstrution;
}
public void setPinstrution(String pinstrution) {
this.pinstrution = pinstrution;
}
public String getImagepath() {
return imagepath;
}
public void setImagepath(String imagepath) {
this.imagepath = imagepath;
}
public int getEdornot() {
return edornot;
}
public void setEdornot(int edornot) {
this.edornot = edornot;
}
}
4.page.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page language="java" import="tdw.beans.*" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>page.jsp' starting page</title>
</head>
<body>b<br>
<%
List<Product> pageList = (List)session.getAttribute("pageList");
int count = (Integer)session.getAttribute("count");
int pageNo = (Integer)session.getAttribute("pageNo");
int pageCount = count/4+1;//共分成多少页
for(Product product:pageList) {%>
<table>
<tr>
<td><%=product.getPname()%></td>
</tr>
<%} %>
</table>
<%if(pageNo==1){
%>
<span>[第一页]</span>
<span>[上一页]</span>
<%
}else{
%>
<span><a href="${pageContext.request.contextPath}/servlet/pageServlet?pageNo=1">[第一页]</a></span>
<span><a href="<span style="font-family: Arial, Helvetica, sans-serif;">${pageContext.request.contextPath}</span>?pageNo=<%=pageNo-1%>">[上一页]</a></span>
<%
}
%>
<%
for(int i=1;i<=pageCount;i++){
if(pageNo==i){
%>
<span><%=i %></span>
<%
}else{
%>
<span><a href="${pageContext.request.contextPath}?pageNo=<%=i%>"></a><%=i %></span>
<%
}
}
%>
<%
if(pageNo==pageCount){
%>
<span>[下一页]</span>
<span>[最后一页]</span>
<%
}else{
%>
<span><a href="${pageContext.request.contextPath}?pageNo=<%=pageNo+1%>">[下一页]</a></span>
<span><a href="${pageContext.request.contextPath}?pageNo=<%=pageCount%>">[最后一页]</a></span>
<%
}
%>
<form action="${pageContext.request.contextPath }/servlet/pageServlet" method="post">
<select name="pageNo">
<%
for(int i=1;i<=pageCount;i++){
%>
<option value="<%=i%>" ><%=i %></option>
<%} %>
</select>
<input type="submit" value="go"></input>
</form>
</body>
</html>