基于jsp的数据展示页面

相关代码

<%--
  Created by IntelliJ IDEA.
  User: 11637
  Date: 2020/4/10
  Time: 13:00
  To change this template use File | Settings | File Templates.
--%>
<%@ page import="java.sql.*" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%!
    int totalpages = 0;      //总页数
    int pageSize = 20;       //每一页展示的数据行数
    int totalRecords = 0;    //总的数据条数
    int xpage = 1;           //当前页
%>
<%
    String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    String DB_URL = "jdbc:mysql://localhost:3306/world";
    String USER = "user";
    String PASS = "password";

    try {
        // 数据库连接
        Class.forName(JDBC_DRIVER);
        Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
        String sql = "SELECT count(*) FROM city"; 
        PreparedStatement psmt = conn.prepareStatement(sql);
        ResultSet rs = psmt.executeQuery();

        rs.next();
        totalRecords = rs.getInt(1);      //获取所有的数据条数
        String aa = request.getParameter("page");  //获取当前页
        if (aa == null || xpage<1) {
            xpage = 1;
        } else {
            try {
                xpage = Integer.parseInt(aa);
            } catch (NumberFormatException e) {
                xpage = 1;
            }
        }

        if (totalRecords % pageSize == 0) {
            totalpages = totalRecords / pageSize;
        } else {
            totalpages = totalRecords / pageSize + 1;
        }
        totalpages = (totalRecords + pageSize - 1) / pageSize;

        //显示当前数据
        sql = "SELECT * FROM city";
        psmt = conn.prepareStatement(sql);
        rs = psmt.executeQuery();

        if (xpage >= totalpages) {
            for (int i = 1; i <= (totalpages - 1) * pageSize; i++) {
                rs.next();
            }
        } else {
            for (int i = 1; i <= (xpage - 1) * pageSize; i++) {
                rs.next();
            }
        }
%>
<!--
    数据展示页面的设计
-->
<html>
<head>
    <title>仿百度页面的分页显示</title>
</head>
<body>
<center>
    <h3>世界城市人口数据</h3>
    <hr width="80%">
    <table border="1" width="80%" align="center">
        <% //数据库调取数据
            for (int i = 1; i <= pageSize; i++) {
                if (!rs.next()) {
                    break;
                }
                int id = rs.getInt(1);
                String name1 = rs.getString(2);
                String countrycode = rs.getString(3);
                String district = rs.getString(4);
                int population = rs.getInt(5);
        %>
<%--        数据展示--%>
        <tr align="center">
            <td style="width: 20%"><%=id%>
            </td>
            <td style="width: 20%"><%=name1%>
            </td>
            <td style="width: 20%"><%=countrycode%>
            </td>
            <td style="width: 20%"><%=district%>
            </td>
            <td style="width: 20%"><%=population%>
            </td>
        </tr>
        <%}%>
    </table>
    <hr>
    <%  //页码标签
        if (xpage <= 6) {
            if (xpage == 1) {
                out.print("首页");
                out.print(" ");
                out.print(" ");
                for (int i = 1; i <= 10; i++) {
                    out.println(" + i + ">" + i + "");
                    out.print(" ");
                    out.print(" ");
                }
                out.print(" ");
                out.print(" ");
                out.print(" + totalpages + ">尾页");
            } else {
                out.print("首页");
                out.print(" ");
                out.print(" ");
                for (int i = 1; i <= 10; i++) {
                    out.println(" + i + ">" + i + "");
                    out.print(" ");
                    out.print(" ");
                }
                out.print(" ");
                out.print(" ");
                out.print(" + totalpages + ">尾页");
            }
        } else {
            if (xpage == totalpages) {
                out.print("首页");
                out.print(" ");
                out.print(" ");
                for (int i = xpage - 5; i <= xpage + 4; i++) {
                    out.println(" + i + ">" + i + "");
                    out.print(" ");
                    out.print(" ");
                }
                out.print(" ");
                out.print(" ");
                out.print("尾页");
            } else {
                out.print("首页");
                out.print(" ");
                out.print(" ");
                for (int i = xpage - 5; i <= xpage + 4; i++) {
                    out.println(" + i + ">" + i + "");
                    out.print(" ");
                    out.print(" ");
                }
                out.print(" ");
                out.print(" ");
                out.print(" + totalpages + ">尾页");
            }
        }
    %>
</center>
<%
    } catch (ClassNotFoundException | SQLException e) {
        e.printStackTrace();
    }
%>
</body>
</html>

实现效果

基于jsp的数据展示页面_第1张图片

你可能感兴趣的:(web_class)