Oracle分页在Web上的首页
本例子代码,只讲Oracle分页的常用的一种方法在Web中的使用,不涉及MVC,代码优化等东西,高手请飘过。新手可以参考。
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
<body>
************************************* Oracle分页的重要代码,学习自 韩顺平老师的 Java操作Oracle 十二讲 **************************************** <br>
<br>
<form action="/TestOracleFenye/index.jsp">
<input type="text" name="pageSize">
<input type="submit" name="submit" value="设置pageSize">
<br/>
</form>
<table>
<tr>
<td>姓名</td>
<td>薪水</td>
</tr>
<%
//加载驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
//建立连接
Connection conn = DriverManager.getConnection(
"jdbc:oracle:thin:@192.168.1.5:1521:BJSXT", "scott","tiger");
//定义结果集
ResultSet rs = null;
//从下面开始和SqlServer 一模一样
Statement sm = conn.createStatement();
//初始化每页的记录条数
int pageSize = 20;
String s_PageSize = request.getParameter("pageSize");
if(s_PageSize!=null && !"".equals(s_PageSize.trim())) {
pageSize = Integer.parseInt(s_PageSize);
}
//总共有多少页,计算得出
int pageCount = 0;
//总共有多少条记录,查询得出
int rowCount = 0;
//当前页
int currentPage = 1;
//初始化开始和结束的记录条数
int start = 1;
int end = pageSize;
//得到记录总数
rs = sm.executeQuery("select count(*) from emp");
while(rs.next()) {
rowCount = rs.getInt(1);
if(rowCount%pageSize==0){
pageCount = rowCount/pageSize;
} else {
pageCount = rowCount/pageSize+1;
}
}
//当前页码
String s_CurrentPage = request.getParameter("currentPage");
if(s_CurrentPage!=null && !"".equals(s_CurrentPage.trim())) {
currentPage = Integer.parseInt(s_CurrentPage);
start = (currentPage-1)*pageSize + 1;
end = currentPage*pageSize;
}
//分页的关键代码
String fenyeSql = "select * from (select a1.*, rownum bieming from (select * from emp) a1 where rownum<=" + end + ") where bieming>=" + start;
out.println(fenyeSql + "<br />");
rs = sm.executeQuery(fenyeSql);
//##查询所有的员工 rs = sm.executeQuery("select * from emp");
while(rs.next()){
out.print("<tr>");
out.print("<td>" + rs.getString(2) + "</td>");
out.print("<td>" + rs.getString(6) + "</td>");
out.print("</tr>");
}
//打印总页数
for(int i=1; i<=pageCount; i++){
out.print("<a href=‘" + basePath + "index.jsp?currentPage=" + i + "&pageSize=" + pageSize +"‘>" + i + "</a> ");
}
rs.close();
sm.close();
conn.close();
%>
</table>
</body>
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。