jsp oracle分頁與sqlserver分頁

先來個sqlserver分頁
數據庫很簡單就一張表
表字段有:
id,主鍵,自動增長
name 組名

直接貼代碼;


<%@ page language="java" import="java.sql.*,java.util.*" pageEncoding="gbk"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String pageNowStr=request.getParameter("pageNow"); //當前是第幾頁
int pageNow=1;
if(pageNowStr!=null){
pageNow=Integer.valueOf(pageNowStr);
}
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>

<body>
<table border="1">
<%
Connection conn=null;
PreparedStatement pst=null;
ResultSet result=null;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DataBaseName=Spring_dataBase","sa","123456");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int pageSize=3; //每頁多少條記錄
int rowCount=0; //總共有多少條記錄
int pageCount=0; //總共有多少頁記錄

String queryPageCount="select count(*) from Group_tbl";
try {
pst=conn.prepareStatement(queryPageCount);
result=pst.executeQuery();
while(result.next()){
rowCount=result.getInt(1); //獲取頁數
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

if(rowCount%pageSize==0){
pageCount=rowCount/pageSize;
}else{
pageCount=rowCount/pageSize+1;
}

System.out.print("pageCount"+pageCount);
String sql="select top "+pageSize+" * from Group_tbl where id not in(select top "+pageSize*(pageNow-1)+"id from Group_tbl)";
try {
pst=conn.prepareStatement(sql);
result=pst.executeQuery();
while(result.next()){
out.print("<tr><td>"+result.getString("id")+"</td><td>"+ result.getString("name")+"</td></tr>");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
result.close();
pst.close();
conn.close();
}
%>
</table>
<%
if(pageNow!=1){
out.print("<a href=sqlserver.jsp?pageNow="+(pageNow-1)+">上一頁</a>   ");
}
for(int i=pageNow;i<pageNow+3;i++){
if(i<=pageCount) //使其不超過總頁數
out.print("<a href=sqlserver.jsp?pageNow="+i+">"+i+"</a>   ");

}
if(pageNow<pageCount)
out.print("<a href=sqlserver.jsp?pageNow="+(pageNow+1)+">下一頁</a>");
%>


<br>
</body>
</html>



效果如下;

[img]http://dl.iteye.com/upload/attachment/577624/649ccfcd-f6f8-36da-ac86-acacf7994277.bmp[/img]


----------------------------------------------------------------------
再來個oracle分頁
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章