JSP数据库分页

第一步先创建数据库连接

public class JndiDemo {
    /*
     * Context文件配置方法
     */
    public static String jndivalue() {
        String jndiValue = "";
        try {
            // 创建上下文配置对象
            Context context = new InitialContext();
            // 获取Tomcoat中的context配置对象
            jndiValue = (String) context.lookup("java:comp/env/jndiName");

            System.out.println(jndiValue);
        } catch (NamingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return jndiValue;

    }

    /*
     * Web.xml文件配置方法
     */
    public static Connection getConn() {
        Connection conn = null;

        try {
            // 获取初始化context实例
            Context initContext = new InitialContext();

            // 根据context实例 lookup实例 lookup数据源名称得到数据源
            DataSource source = (DataSource) initContext.lookup("java:comp/env/DBResource");

            // 通过数据源DataSource获取数据库连接
            conn = source.getConnection();

        } catch (NamingException | SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return conn;
    }

    /*
     * 关闭数据库
     */
    public static void close(Connection conn, PreparedStatement pro, ResultSet result) {

        try {

            if (conn != null) {
                conn.close();
            }
            if (pro != null) {
                pro.close();
            }
            if (result != null) {
                result.close();
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace()  }}}

第二步在创建一个实体类

public class NewsEntity {

    int sid = 0;// 员工编号
    String sname = null;// 员工姓名
    String site = null;// 员工地址
    int salary = 0;// 员工工资
    String gender = null;// 员工性别
    String rank = null;// 员工级别

    public int getSid() {
        return sid;
    }

    public void setSid(int sid) {
        this.sid = sid;
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public String getSite() {
        return site;
    }

    public void setSite(String site) {
        this.site = site;
    }

    public int getSalary() {
        return salary;
    }

    public void setSalary(int salary) {
        this.salary = salary;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getRank() {
        return rank;
    }

    public void setRank(String rank) {
        this.rank = rank;
    }

}
 

第三步在创建连接显示方法类

public interface NewsDao {

    // 显示方法
    public int NewsShow();

    /*
     * 显示方法
     * 
     * start 表示开始数据条。既第几条数据
     *
     * pageSize 表示显示多少条数据。既从第几条数据到几条数据
     * 
     */
    public List<NewsEntity> NewsShow(int start, int pageSize);

}

第四步在创建一个方法实现类

public class NewsDaoImpl implements NewsDao {

    /*
     * 显示方法
     */
    public int NewsShow() {
        String sql = "SELECT count(sid) count FROM  employee";

        Connection conn = JndiDemo.getConn();
        PreparedStatement pro = null;// 执行sql
        ResultSet result = null;// 结果集对象

        int count = 0;
        try {
            pro = conn.prepareStatement(sql);
            result = pro.executeQuery();// 返回一个结果集
            while (result.next()) {
                count = result.getInt("count");
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            JndiDemo.close(conn, pro, result);// 关闭数据库
        }

        return count;
    }

       /*
     * 显示方法
     * 
     * start 表示开始数据条。既第几条数据
     *
     * pageSize 表示显示多少条数据。既从第几条数据到几条数据
     * 
     */

public List<NewsEntity> NewsShow(int start, int pageSize) {
        // limit用于截取数据,截取数据的格式是从第几条数据,到之后的第几条数据
        String sql = "SELECT *  FROM  employee limit " + (start - 1) + "," + pageSize;

        Connection conn = JndiDemo.getConn();
        PreparedStatement pro = null;// 执行sql
        ResultSet result = null;// 结果集对象

        List<NewsEntity> list = new ArrayList<NewsEntity>();// 使用集合把数据库里面的数据存储在集合里面

        try {
            pro = conn.prepareStatement(sql);
            result = pro.executeQuery();
            while (result.next()) {
                NewsEntity sta = new NewsEntity();
                sta.setSid(result.getInt("sid"));
                sta.setSname(result.getString("sname"));
                sta.setSite(result.getString("site"));
                sta.setSalary(result.getInt("salary"));
                sta.setGender(result.getString("gender"));
                sta.setRank(result.getString("rank"));
                list.add(sta);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JndiDemo.close(conn, pro, result);// 关闭数据库
        }

        return list;
    }
 

第五步在创建一个分页通用类

public class Page {

    // 总行数
    private int rowTotal;

    // 每页显示多少行 默认为100条
    private int pageSize = 5;

    // 当前页
    private int current;

    // 总页数
    private int total;

    // 开始页
    private int startIndex;

    // 结束页
    private int endIndex;

    /**
     * 
     * @param rowTotal
     *            总数据
     * @param currentPage
     *            当前页
     */
    public Page(int rowTotal, int currentPage) {
        this.rowTotal = rowTotal;

        this.current = currentPage;

        this.calculate();
    }

    /**
     * 
     * @param rowTotal
     *            总行数
     * @param currentPage
     *            当前页
     * @param pageSize
     *            每页显示多少条数据
     */
    public Page(int rowTotal, int currentPage, int pageSize) {
        
        this.rowTotal = rowTotal;
        
        this.current = currentPage;
        
        this.pageSize = pageSize;
        // 调用计算方法
        this.calculate();
    }

    // 计算当前的页数
    private void calculate() {

        // 计算总页数 =总记录除以 每页显示多少条记录 如果能整除 则不需要+1 否则+1
        this.total = this.rowTotal / this.pageSize + ((this.rowTotal % this.pageSize) > 0 ? 1 : 0);

        // 如果当前页大于 总页数 说明已经到了最后一页 那么当前页等于最后一页
        if (current > total) {
            current = total;
        }
        // 如果当前页是0 则等于第一页
        if (current < 1) {
            current = 1;
        }

        // 计算开始记录 =等于当前页-1 乘以每页显示大小,再加1
        startIndex = (this.current - 1) * this.pageSize + 1;

        // 计算 查询到第多少行
        endIndex = this.current * this.pageSize;

        // 如果查询的行数大于总行数 那么则 查询结束的行等于总行数
        if (endIndex > this.rowTotal) {
            endIndex = this.rowTotal;
        }

    }

    public int getRowTotal() {
        return rowTotal;
    }

    public void setRowTotal(int rowTotal) {
        this.rowTotal = rowTotal;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getCurrent() {
        return current;
    }

    public void setCurrent(int current) {
        this.current = current;
    }

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }

    public int getStartIndex() {
        return startIndex;
    }

    public void setStartIndex(int startIndex) {
        this.startIndex = startIndex;
    }

    public int getEndIndex() {
        return endIndex;
    }

    public void setEndIndex(int endIndex) {
        this.endIndex = endIndex;
    }

}

第六步创建一个JSP分页实现类并调用分页通用类和方法实现类的方法

<title>分页显示类</title>
</head>
<body>

    <%
        //新闻对象
        NewsDao news = new NewsDaoSerivce

        String pagesattach = request.getParameter("pages");//接收当前页数


        //判断是否等于空。如果等于空则显示第一个页面,否则显示大于第一个的页面
        if (pagesattach == null || Integer.parseInt(pagesattach) <= 0) {

            pages = 1;
        } else {
            //强制类型转换,并接收
            pages = Integer.parseInt(pagesattach);
        }

        int count = news.showNews();//得到的总页数,

        //创建分页类,计算得到总数据,当前页数,每页该显示多少条数据
        Page pa = new Page(count, pages, 5);

        //新闻集合实体类对象,‘pa.getStartIndex()’表示从第数据几条开始显示数据,‘pa.getPageSize()’表示从第几条结束显示的数据
        List<NewsEntity> list = news.showNews(pa.getStartIndex(), pa.getPageSize());
    %>

    <!-- 循环遍历并显示数据 -->
    <%
        for (NewsEntity lis : list) {
    %>

    <div>
        员工姓名:<%=lis.getSname()%>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span>
            员工地址:<%=lis.getSite()%></span>
    </div>

    <%
        }
    %>

    <div>
        <!-- 点击此‘A’标签可进入页面中的第一页-->
        <a href="?pages=0">首页</a>&nbsp;

        <!-- 判断当前页如歌等于‘1’ 则进入判断中的下一页,否则进入‘A’标签,并且该‘A’标签可点击下一页,并可切换页面 -->
        <%
            if (pages == 1) {
        %>
        上一页
        <%
            } else {
        %>
        <!-- pages表示当前页, -->
        <a href="?pages=<%=pages - 1 <= 0 ? 1 : pages - 1%>">上一页</a>
        <%
            }
        
            //判断当前页是否等于总页数,如果等于进入‘下一页’否则进入‘a’标签,并且该‘A’标签可点击下一页,并可切换页面,
            if (pages == pa.getTotal()) {
        %>
        下一页
        <%
            } else {
        %>
        <a
            href="?pages=<%=pages + 1 > pa.getTotal() ? pa.getTotal() : pages + 1%>">下一页</a>&nbsp;
        <%
            }
        %>

        <!-- 点击此‘A’标签可跳转到所以页面的最后一页 -->
        <a href="?pages=<%=pa.getTotal()%>">尾页</a>

        <!-- 显示该页数的当前页数 -->
        &nbsp;&nbsp;当前页数:<%=pages%>
        <!-- 显示该页数的总页数 -->
        &nbsp;&nbsp;总页数:<%=pa.getTotal()%>

    </div>


</body>
</html>

猜你喜欢

转载自blog.csdn.net/qq_42358704/article/details/81293842