JSP 页面实现数据分页

字体大小: 中小 标准 ->行高大小: 标准
<%@ page import="java.sql.*" %>
<%--
  Created by IntelliJ IDEA.
  User: lx_sunwei
  Date: 14-1-4
  Time: 下午5:45
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    int maxPage = 0  //一共有多少页
            ,maxRowCount     //一共有多少行
            ,rowsPerPage     //每页显示多少行
            ,curPage;        //当前页页码
    Connection conn = null;  //数据库连接对象
    Statement st = null;     //SQL语句对象
    ResultSet rs = null;     //结果集对象

    rowsPerPage = 5;         //设置每页显示5行

    String curpage1 = request.getParameter("page");  //取得当前页页码
    if (curpage1 == null){  //若没有page参数
        curPage = 1;
    }else {
        curPage = Integer.parseInt(curpage1);  //字符串转换为整数
        if (curPage < 1)
            curPage = 1;
    }

    String url = "jdbc:oracle:thin:@localhost:1521:orcl";  //连接字符串
    try {
        Class.forName("oracle.jdbc.driver.OracleDriver");  //装载驱动
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
    try {
        conn = DriverManager.getConnection(url,"scott","tiger");  //创建连接对象
        st = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);  //设置结果集
        String sql = "select * from emp";  //SQL语句
        rs = st.executeQuery(sql);  //执行查询

        rs.last();  //移动光标到结果集最后一行
        maxRowCount = rs.getRow();  //返回当前行的行号 即总共的行数
        maxPage = (maxRowCount + rowsPerPage - 1) / rowsPerPage;  //总页数
        if (curPage > maxPage)
            curPage = maxPage;
    } catch (SQLException  e) {
        e.printStackTrace();
    }
%>
<html>
<head>
    <meta http-equiv="content-type" content="text/html" charset="utf-8">
    <title>JSP分页</title>
    <link rel="stylesheet" type="text/css" href="css.css">
</head>
<body>
<div style="margin-left: 30%;margin-top: 10%">
    <table>
        <caption>SCOTT用户,EMP表中的数据</caption>
        <tr>  <!--表头-->
            <th>EMPNO</th>
            <th>ENAME</th>
            <th>JOB</th>
            <th>MGR</th>
            <th>HIREDATE</th>
            <th>SAL</th>
            <th>COMM</th>
            <th>DEPTNO</th>
        </tr>
        <%
            if (maxPage > 0)  {
                try {
                    rs.absolute((curPage - 1) * rowsPerPage + 1);  //移动光标到当前页的第一行
                    int i = 0;
                    while (i < rowsPerPage && !rs.isAfterLast()){  //循环输出当前的数据
        %>
        <tr>
            <td><%= rs.getBigDecimal("EMPNO") %></td>
            <td><%= rs.getString("ENAME") %></td>
            <td><%= rs.getString("job") %></td>
            <td><%= rs.getBigDecimal("MGR") %></td>
            <td><%= rs.getDate("HIREDATE") %></td>
            <td><%= rs.getBigDecimal("SAL") %></td>
            <td><%= rs.getBigDecimal("COMM") %></td>
            <td><%= rs.getBigDecimal("DEPTNO")%></td>
        </tr>
        <%if (rs.next()){
            i++;
        }
        }
        %>
        <%
                    rs.close();
                    st.close();
                    conn.close();

                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        %>
    </table>
</div>
<div style="text-align: center; margin-top: 10%" >
    第<%=curPage%>页,共<%=maxPage%>页&nbsp;&nbsp;&nbsp;
    <%--第一页不能向前翻,最后一页不能向后翻。点击首页、尾页跳至第一页、最后一页--%>
    <%if (curPage > 1)
    {%>
    <a href="pagemain.jsp?page=1">首页</a>
    <a href="pagemain.jsp?page=<%=curPage - 1%>">上一页</a>
    <%}else {%>
    首页 上一页   <!--不能被触发-->
    <%}%>&nbsp;

    <%if (curPage < maxPage){%>
    <a href="pagemain.jsp?page=<%=curPage + 1 %>">下一页</a>
    <a href="pagemain.jsp?page=<%= maxPage %>">尾页</a>
    <%}else {%>
    下一页 尾页  <!--不能被触发-->
    <%}%>&nbsp;&nbsp;&nbsp;转至第&nbsp;<form name="form1" action="pagemain.jsp" method="get">
    <label>
        <select name="page" onchange="document.form1.submit()">
            <%
                for (int j = 1; j <= maxPage; j++) {
                    if (j == curPage) {
            %>
            <option selected value=<%=j  //当前页页码默认选中
            %>><%= j %></option>
            <%} else {%>
            <option value=<%= j
            %>><%= j%></option>
            <%
                    }
                }
            %>
        </select>&nbsp;页
    </label>
</form>
</div>
</body>
</html>

此文章由 http://www.ositren.com 收集整理 ,地址为: http://www.ositren.com/htmls/69054.html