http://my.oschina.net/xiaomaoandhong/blog/74585
以scott方案下emp表为例:
JdbcOdbcConnection.java
01 | package cn.nevo.service; |
03 | import java.sql.Connection; |
04 | import java.sql.DriverManager; |
05 | import java.sql.SQLException; |
08 | * @author Administrator |
10 | public class JdbcOdbcConnection { |
12 | public JdbcOdbcConnection(String driver) { |
15 | Class.forName(driver); |
16 | } catch (ClassNotFoundException e) { |
21 | public Connection getConnection(String url, String user, String pwd) { |
22 | Connection conn = null ; |
24 | conn = DriverManager.getConnection(url, user, pwd); |
25 | } catch (SQLException e) { |
<!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin. http://dunnhq.com -->
OraclePaging.java
01 | package cn.nevo.service; |
03 | import java.sql.Connection; |
04 | import java.sql.ResultSet; |
05 | import java.sql.SQLException; |
06 | import java.sql.Statement; |
08 | public class OraclePaging { |
11 | final String driver = "oracle.jdbc.driver.OracleDriver" ; |
12 | final String url = "jdbc:oracle:thin:@localhost:1521:orcl" ; |
13 | final String user = "scott" ; |
14 | final String pwd = "tiger" ; |
16 | Connection conn = new JdbcOdbcConnection(driver).getConnection(url, user, pwd); |
21 | public ResultSet oraclePage(String strsql) { |
23 | st = conn.createStatement(); |
24 | rs = st.executeQuery(strsql); |
26 | } catch (SQLException e) { |
33 | public void closeAll() { |
38 | } catch (SQLException e) { |
<!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin. http://dunnhq.com -->
index.jsp
01 | <%@ page language= "java" import = "java.util.*" pageEncoding= "UTF-8" %> |
02 | <% @page import = "java.sql.ResultSet" %> |
04 | String path = request.getContextPath(); |
05 | String basePath = request.getScheme()+ "://" +request.getServerName()+ ":" +request.getServerPort()+path+ "/" ; |
08 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" > |
11 | <base href= "<%=basePath%>" > |
13 | <title>查询scott方案下emp表实现分页</title> |
14 | <meta http-equiv= "pragma" content= "no-cache" > |
15 | <meta http-equiv= "cache-control" content= "no-cache" > |
16 | <meta http-equiv= "expires" content= "0" > |
17 | <meta http-equiv= "keywords" content= "keyword1,keyword2,keyword3" > |
18 | <meta http-equiv= "description" content= "This is my page" > |
20 | <link rel= "stylesheet" type= "text/css" href= "styles.css" > |
25 | <jsp:useBean id= "oraclePaging" class = "cn.nevo.service.OraclePaging" /> |
26 | <h2 align= "center" >实现oracle分页</h2> |
27 | <table border= "1" cellpadding= "0" cellspacing= "0" align= "center" width= "500" > |
29 | <th>姓名</th><th>入职日期</th> |
34 | String s_pageNow = (String)request.getParameter( "pageNow" ); |
36 | if (! "" .equals(s_pageNow)&&s_pageNow != null ){ |
37 | pageNow = Integer.parseInt(s_pageNow); |
43 | String strsql2 = "select count(*) from emp" ; |
48 | rs = (oraclePaging.oraclePage(strsql2)); |
50 | recordCount = rs.getInt( 1 ); |
51 | if (recordCount % rowcount == 0 ) { |
52 | pagecount = recordCount / rowcount; |
54 | pagecount = recordCount / rowcount + 1 ; |
59 | String strsql = "select * from (select e.*, rownum rn from (select * from emp) e where rownum <= " + rowcount * pageNow + ") where rn >=" + ((pageNow - 1 ) * (rowcount) + 1 ); |
61 | rs = oraclePaging.oraclePage(strsql); |
64 | out.println( "<td align = 'center'>" + rs.getString( 2 ) + "</td>" ); |
65 | out.println( "<td align = 'center'>" + rs.getDate( 5 ) + "</td>" ); |
73 | for ( int i= 1 ; i<=pagecount; i++) { |
74 | out.print( "<a href = index.jsp?pageNow=" + i + ">[" + i + "]</a>" ); |
77 | oraclePaging.closeAll(); |
<!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin. http://dunnhq.com -->
启动Oracle服务器,通过http://localhost:8080/OraclePagingTest/访问示例: