下面的是分页代码。运行 的结果没有什么异常,但是也不出现页面。很恼火。///PageBean.javapackage fenye;import java.util.Vector;public class PageBean {
public int curPage; //当前是第几页
public int maxPage; //一共有多少页
public int maxRowCount; //一共有多少行
public int rowsPerPage=5; //每页多少行
public java.util.Vector data; //本页中要显示的资料
public PageBean() {
} public void countMaxPage() {
if (this.maxRowCount % this.rowsPerPage == 0) {
this.maxPage = this.maxRowCount / this.rowsPerPage;
} else {
this.maxPage = this.maxRowCount / this.rowsPerPage + 1;
}
} public Vector getResult() {
return this.data;
} public PageBean(ContactBean contact)throws Exception{
this.maxRowCount=contact.getAvailableCount();
this.data=contact.getResult();
this.countMaxPage();
}
}///ContactBean.javapackage fenye;import java.util.*;
import java.sql.*;
public class ContactBean {
private Connection conn;
Vector v;
public ContactBean() throws Exception {
conn = DataBaseConn.getConnection();
v = new Vector();
} public int getAvailableCount() throws Exception {
int ret = 0;
Statement stmt = conn.createStatement();
String strSql = "select count(*) from book";
ResultSet rs = stmt.executeQuery(strSql);
while (rs.next()) {
ret = rs.getInt(1);
}
return ret;
} public PageBean listData(String page) throws Exception {
try {
PageBean pageBean = new PageBean(this);
int pageNum = Integer.parseInt(page);
Statement stmt = conn.createStatement(); String strsql = "select top " + pageNum * pageBean.rowsPerPage +
"* from book order by id";
ResultSet rs = stmt.executeQuery(strsql);
int i = 0;
while (rs.next()) {
if (i > (pageNum - 1) * pageBean.rowsPerPage - 1) {
Object[] obj = new Object[6];
obj[0] = rs.getString("BookDB");
obj[1] = rs.getString("Pages");
obj[2] = rs.getString("Price");
obj[3] = rs.getString("Title");
obj[4] = rs.getString("Author");
obj[5] = rs.getString("BookMemo");
v.add(obj);
}
i++;
}
rs.close();
stmt.close(); pageBean.curPage = pageNum;
pageBean.data = v;
return pageBean;
} catch (Exception e) {
e.printStackTrace();
throw e;
}
} public Vector getResult() throws Exception {
return v;
}
}
///contactServlet.javapackage fenye;import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;public class ContactServlet extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK"; //Initialize global variables
public void init() throws ServletException {
} //Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
try {
ContactBean contact = new ContactBean();
PageBean pageCtl = contact.listData((String) request.getParameter(
"jumpPage"));
request.setAttribute("pageCtl", pageCtl);
} catch (Exception e) {
e.printStackTrace();
}
javax.servlet.RequestDispatcher dis = request.getRequestDispatcher(
"/contact");
dis.forward(request, response);
} //Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doGet(request, response);
} //Clean up resources
public void destroy() {
}
}
///contact.jsp<%@page contentType="text/html; charset=GBK"%>
<jsp:useBean id="pageCtl" class="fenye.PageBean" scope="request"/>
<table border="1">
<%
java.util.Vector v = pageCtl.getResult();
java.util.Enumeration e = v.elements();
while (e.hasMoreElements()) {
Object[] obj = (Object[]) e.nextElement();
%>
<tr>
<td><%=obj[0] %> </td>
<td><%=obj[1] %> </td>
<td><%=obj[2] %> </td>
<td><%=obj[3] %> </td>
<td><%=obj[4] %> </td>
<td><%=obj[5] %> </td>
</tr>
<%}%>
</table>
<%if (pageCtl.maxPage != 1) {%>
<form name="PageForm" action="/ContactServlet" method="post">
<script language="javascript" type="">
<!--
function Jumping(){
document.PageForm.submit();
return;
}function gotoPage(pagenum){
document.PageForm.jumpPage.value=pagenum;
document.PageForm.submit();
return;
}
-->
</script>
每页
<%=pageCtl.rowsPerPage %>
行
共
<%=pageCtl.maxRowCount %>
行
第
<%=pageCtl.curPage %>
页
共
<%=pageCtl.maxPage %>页
<br>
<%
if (pageCtl.curPage == 1) {
out.print(" 首页 上一页");
}
else {
%>
<A href="javascript:gotoPage(1)">首页</a>
<A href="javascript:gotoPage(<%=pageCtl.curPage-1%>">上一页</a>
<%}%>
<%
if (pageCtl.curPage == 1) {
out.print(" 下一页 尾页");
}
else {
%>
<A href="javascript:gotoPage(<%=pageCtl.curPage+1 %>)">下一页</a>
<A href="javascript:gotoPage(<%=pageCtl.maxPage-1%>">尾页</a>
<%}%>
转到第
<select name="jumpPage" onchange="Jumping()">
<%
for (int i = 1; i <= pageCtl.maxPage; i++) {
if (i == pageCtl.curPage) {
%>
<option selected value=<%=i %>><%=i %></option>
<%} else {%>
<option value=<%=i%>><%=i %></option>
<%}}%>
</form>
<%}%>
public int curPage; //当前是第几页
public int maxPage; //一共有多少页
public int maxRowCount; //一共有多少行
public int rowsPerPage=5; //每页多少行
public java.util.Vector data; //本页中要显示的资料
public PageBean() {
} public void countMaxPage() {
if (this.maxRowCount % this.rowsPerPage == 0) {
this.maxPage = this.maxRowCount / this.rowsPerPage;
} else {
this.maxPage = this.maxRowCount / this.rowsPerPage + 1;
}
} public Vector getResult() {
return this.data;
} public PageBean(ContactBean contact)throws Exception{
this.maxRowCount=contact.getAvailableCount();
this.data=contact.getResult();
this.countMaxPage();
}
}///ContactBean.javapackage fenye;import java.util.*;
import java.sql.*;
public class ContactBean {
private Connection conn;
Vector v;
public ContactBean() throws Exception {
conn = DataBaseConn.getConnection();
v = new Vector();
} public int getAvailableCount() throws Exception {
int ret = 0;
Statement stmt = conn.createStatement();
String strSql = "select count(*) from book";
ResultSet rs = stmt.executeQuery(strSql);
while (rs.next()) {
ret = rs.getInt(1);
}
return ret;
} public PageBean listData(String page) throws Exception {
try {
PageBean pageBean = new PageBean(this);
int pageNum = Integer.parseInt(page);
Statement stmt = conn.createStatement(); String strsql = "select top " + pageNum * pageBean.rowsPerPage +
"* from book order by id";
ResultSet rs = stmt.executeQuery(strsql);
int i = 0;
while (rs.next()) {
if (i > (pageNum - 1) * pageBean.rowsPerPage - 1) {
Object[] obj = new Object[6];
obj[0] = rs.getString("BookDB");
obj[1] = rs.getString("Pages");
obj[2] = rs.getString("Price");
obj[3] = rs.getString("Title");
obj[4] = rs.getString("Author");
obj[5] = rs.getString("BookMemo");
v.add(obj);
}
i++;
}
rs.close();
stmt.close(); pageBean.curPage = pageNum;
pageBean.data = v;
return pageBean;
} catch (Exception e) {
e.printStackTrace();
throw e;
}
} public Vector getResult() throws Exception {
return v;
}
}
///contactServlet.javapackage fenye;import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;public class ContactServlet extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK"; //Initialize global variables
public void init() throws ServletException {
} //Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
try {
ContactBean contact = new ContactBean();
PageBean pageCtl = contact.listData((String) request.getParameter(
"jumpPage"));
request.setAttribute("pageCtl", pageCtl);
} catch (Exception e) {
e.printStackTrace();
}
javax.servlet.RequestDispatcher dis = request.getRequestDispatcher(
"/contact");
dis.forward(request, response);
} //Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doGet(request, response);
} //Clean up resources
public void destroy() {
}
}
///contact.jsp<%@page contentType="text/html; charset=GBK"%>
<jsp:useBean id="pageCtl" class="fenye.PageBean" scope="request"/>
<table border="1">
<%
java.util.Vector v = pageCtl.getResult();
java.util.Enumeration e = v.elements();
while (e.hasMoreElements()) {
Object[] obj = (Object[]) e.nextElement();
%>
<tr>
<td><%=obj[0] %> </td>
<td><%=obj[1] %> </td>
<td><%=obj[2] %> </td>
<td><%=obj[3] %> </td>
<td><%=obj[4] %> </td>
<td><%=obj[5] %> </td>
</tr>
<%}%>
</table>
<%if (pageCtl.maxPage != 1) {%>
<form name="PageForm" action="/ContactServlet" method="post">
<script language="javascript" type="">
<!--
function Jumping(){
document.PageForm.submit();
return;
}function gotoPage(pagenum){
document.PageForm.jumpPage.value=pagenum;
document.PageForm.submit();
return;
}
-->
</script>
每页
<%=pageCtl.rowsPerPage %>
行
共
<%=pageCtl.maxRowCount %>
行
第
<%=pageCtl.curPage %>
页
共
<%=pageCtl.maxPage %>页
<br>
<%
if (pageCtl.curPage == 1) {
out.print(" 首页 上一页");
}
else {
%>
<A href="javascript:gotoPage(1)">首页</a>
<A href="javascript:gotoPage(<%=pageCtl.curPage-1%>">上一页</a>
<%}%>
<%
if (pageCtl.curPage == 1) {
out.print(" 下一页 尾页");
}
else {
%>
<A href="javascript:gotoPage(<%=pageCtl.curPage+1 %>)">下一页</a>
<A href="javascript:gotoPage(<%=pageCtl.maxPage-1%>">尾页</a>
<%}%>
转到第
<select name="jumpPage" onchange="Jumping()">
<%
for (int i = 1; i <= pageCtl.maxPage; i++) {
if (i == pageCtl.curPage) {
%>
<option selected value=<%=i %>><%=i %></option>
<%} else {%>
<option value=<%=i%>><%=i %></option>
<%}}%>
</form>
<%}%>
你用javascript来代替它
第一,实力华她。然后再跳用它就好了
public List executeQuery(String sql,String[] cols,int pageSize,int nPage) throws SQLException
{
Connection conn=sfca.common.ConnectionManager.getConnection();
Statement stmt=null;
ResultSet rs=null;
ArrayList AL=new ArrayList();
ArrayList returnList=new ArrayList();
int size=cols.length;
int maxPage=1;
try
{
try
{
stmt=conn.createStatement();
try
{
rs=stmt.executeQuery(sql);
while(rs.next())
{
String[] str=new String[size];
for(int i=0;i<size;i++)
{
str[i]=rs.getString(cols[i]);
}
AL.add(str);
}
}
finally
{
rs.close();
rs=null;
}
}
finally
{
stmt.close();
stmt=null;
}
}
finally
{
conn.close();
conn=null;
}int maxSize = AL.size();
System.out.println("Exception:maxSize:"+maxSize);int startIndex=(nPage-1)*pageSize;
int endIndex=startIndex+pageSize;if(startIndex<0)
startIndex=0;if(endIndex>maxSize)
endIndex= maxSize;
//System.out.println("Exception:startIndex:"+startIndex);
//System.out.println("Exception:endIndex:"+endIndex);
return(AL.subList(startIndex,endIndex)); }%>
调用: List list=executeQuery(sql,new String[]{"productid","userid","title","created"},perPage,nPage);sql是查询语句,String[]是要取出的字段名, perPage是每页显示多少条记录,nPage是当前是第几页。
源码 你都调不出来