Oracle学习笔记(十)——分页显示简单示例

分页显示emp表的用户信息

<%@ page language="java" import="java.util.*,java.sql.*" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h2>oracle分页案例</h2>
<table>
<tr><td>用户名</td>><td>薪水</td></tr>
<%
//1、加载驱动
Class. forName("oracle.jdbc.driver.OracleDriver");

//2、得到连接
Connection ct=DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl", "scott", "liupeiling1123");
//以下部分和sql server部分一模一样
Statement sm=ct.createStatement();

//接受pageNow
String s_pageNow=request.getParameter("pageNow");
int pageNow=1;
if(s_pageNow!=null){
	pageNow=Integer.parseInt(s_pageNow);
}

//查询总页数
int pageCount=0;
int rowCount=0;//总共有几条记录
int pageSize=3;//每页显示几条记录

ResultSet rs=sm.executeQuery("select count(*) from emp");

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

rs=sm.executeQuery( "select * from (select a1.*,rownum rn from (select * from emp) a1 where rownum<="+pageSize*pageNow+") where rn>="+(pageSize*(pageNow-1)+1));

while(rs.next()){
	out.println("<tr>");
      //用户名
    out.println( "<td>"+rs.getString(2)+"</td>");
    out.println( "<td>"+rs.getString(6)+"</td>");
    out.println("</tr>");
}

//打印总页数
for(int i=1;i<=pageCount;i++){
	out.print("<a href=Mytest.jsp?pageNow="+i+"> ["+i+"] </a>");
}

//关闭打开的资源
rs.close();
sm.close();
ct.close();

%>
</table>
</body>
</html>


你可能感兴趣的:(oracle,数据库,jsp,分页,韩顺平)