50分在线等.解决给分!
///PageBean.java
package 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.java
package 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.java
package 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>
<%}%>

