|
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="java.sql.*"%>
<%@ page import="java.lang.Integer"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<script src="<%=request.getContextPath()%>/page.js"></script>
<script type="text/javascript">
</script>
</head>
<body>
<br>
<form name="testPageForm">
<%
int pageCount = 1;//总页数,初始值为1;
int pageSize = 2;//每页的条数
int curPage = 1;//当前页
try {
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
Connection con = null;
try {
con = DriverManager.getConnection(
"jdbcracle:thin192.168.20.66:1521:chshsid","aradmin", "123456“
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
Statement stmt = null;
try {
stmt = con.createStatement();
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
int i = 1;
String pages = request.getParameter("page");
curPage = (pages == null) ? (1) : Integer.parseInt(pages);
String sql = "select count(*) from arschema ";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next())
i = rs.getInt(1);
int recordCount = i;
pageCount = (i % pageSize == 0) ? (i / pageSize)
: (i / pageSize + 1);
int nextPage;
int upPage;
nextPage = curPage + 1;
if (nextPage >= pageCount)
nextPage = pageCount;
upPage = curPage - 1;
if (upPage <= 1)
upPage = 1;
rs.close();
sql = "select * from testPage";
rs = stmt.executeQuery(sql);
i = 0;
out
.println("<table align='center' bgcolor='yellow' border='1' height='400' width='600'>");
while ((i < pageSize * curPage) && rs.next()) {
if (i >= (pageSize * (curPage - 1))) {
out.println("<tr><td>" + rs.getString(1) + "</td><td>"
+ rs.getString(2) + "</td></tr>");
}
i++;
}
rs.close();
stmt.close();
con.close();
%>
<tr>
<td colspan="2">
总页数:<%=curPage%>/<%=pageCount%><a href="index.jsp?page=1">第一页</a>
<a href="index.jsp?page=<%=upPage%>">上一页</a>
<%
for (int j = 1; j <= pageCount; j++) {
if (curPage != j) {
%>
<a href="index.jsp?page=<%=j%>">[<%=j%>]</a>
<%
} else {
out.println(j);
}
}
%>
<a href="index.jsp?page=<%=nextPage%>">下一页</a>
<a href="index.jsp?page=<%=pageCount%>">最后页</a>
</td>
</tr>
<%
out.println("</table>");
%>
<table cellpadding=0 cellspacing=0 border="1" align="center">
<tr>
<td>
<script language="javascript" type="text/javascript">
var p_pageStr = "<%=request.getContextPath()%>/index.jsp?page=_page_&pageSize=<%=pageSize%>"; //路径
document.write(showListPage('<%=recordCount%>','<%=pageSize%>','<%=pageCount%>',<%=curPage%>,p_pageStr,'testPageForm'))
</script>
</td>
</tr>
</table>
</form>
</body>
</html>
2.这个js是几年前专门来分页的js,拿来给大家分享一下。
//翻页
function showListPage(recordCount, pageSize, pageCount, curPage, pageStr,frmpage){
if(pageCount<1) pageCount =0;
if(curPage<1) curPage = 0;
str = '共 <B>'+recordCount+'</B> 条记实,当前 <B>'+curPage+'/'+pageCount+'</B> 页';
if(curPage<=1)
str += " <span style='color:#999999' >首页</span> ";
else
str += " <A href='"+pageStr.replace("_page_",1)+"' class=\"op\">首页</A> ";
if(curPage<=1)
str += " <span style='color:#999999' >上一页</span>";
else
str += " <A href='"+pageStr.replace("_page_",(curPage-1))+"' class=\"op\">上一页</A> ";
if(curPage>=pageCount)
str += " <span style='color:#999999' >下一页</span> ";
else
str += " <A href='"+pageStr.replace("_page_",(curPage+1))+"' class=\"op\">下一页</A> ";
if(curPage>=pageCount)
str += " <span style='color:#999999' >尾页</span> ";
else
str += " <A href='"+pageStr.replace("_page_",pageCount)+"'>尾页</A> ";
str += '第 <B>'+curPage+'/'+pageCount+'</B> 页 <B>'+pageSize+'</B> 条记实/页 共 <B>'+recordCount+'</B> 条记实';
str += " 跳到 <select id=\"txtpage\" name=\"txtpage\" onChange=\"pagego(this.value,"+curPage+","+pageCount+",'"+pageStr+"')\">";
for(i=1; i<=pageCount; i++)
if(curPage==i)
str += "<option value='" + i + "' selected >" + i + "</option>";
else
str += "<option value='" + i + "'>" + i + "</option>";
str += "</select>页";
return str;
}
//翻页的跳转
function pagego(pageGo,curPage,pageCount,pageStr){
pageStr = encodePageURI(pageStr);
if(pageGo>=1 && pageGo<=pageCount && curPage!=pageGo)
window.location = pageStr.replace("_page_", pageGo);
}
//encodeURIComponent访问的路径
function encodePageURI(str){
var result = "";
for( i=0; i < str.length;) {
var aLetter = "";
var number = str.charCodeAt(i);
if(document.all && number>127){
for(j=i+1; j<=str.length; j++){
if(str.charCodeAt(j)<=127)
break;
}
result += encodeURIComponent(str.substring(i, j));
i = j;
}else if(number<=127){
result += str.substring(i, i+1);
i = i+1;
}else{
result += encodeURIComponent(str.substring(i, i+1));
i = i+1;
}
}
return result;
} |
|