使用JNDI实现分页功能

配置JNDI

        打开tomact文件找到conf-->context.xml-->配置

<Environment name="tjndi" value="hello jndi" type="java.lang.String"/>
	<Resource name="jdbc/student" auth="Container" type="javax.sql.DataSource"
   maxActive="100" maxIdle="30" maxWait="10000" username="root"
   password="root" driverClassName="com.mysql.jdbc.Driver"
   url="jdbc:mysql://127.0.0.1:3306/yinhe?
           useUnicode=true&amp;characterEncoding=utf-8" />

1.先配置依赖

<dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>4.0.1</version>
    </dependency>
    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>jsp-api</artifactId>
      <version>2.2</version>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>
    <dependency>
      <groupId>taglibs</groupId>
      <artifactId>standard</artifactId>
      <version>1.1.2</version>
    </dependency>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.47</version>
    </dependency>
    <dependency>
      <groupId>commons-fileupload</groupId>
      <artifactId>commons-fileupload</artifactId>
      <version>1.4</version>
    </dependency>
    <dependency>
      <groupId>commons-io</groupId>
      <artifactId>commons-io</artifactId>
      <version>2.10.0</version>
    </dependency>
  </dependencies>

2.配置xml文件,在web文件中添加servlet映射

 3.搭建框架

4.使用util连接数据库

package com.yhzz.util;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

//连接数据库关闭数据库
public class BaseDaoUtil {
    /*获取数据库连接*/
    public static Connection getConn(){
        try {
            Context context = new InitialContext();
            DataSource dataSource = (DataSource)context.lookup("java:comp/env/jdbc/student");
            return dataSource.getConnection();
        } catch (NamingException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    /*关闭数据库连接*/
    public static void closeAll(Connection connection, PreparedStatement statement, ResultSet resultSet){
        if (resultSet!=null){
            try {
                resultSet.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (statement!=null){
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (connection!=null){
            try {
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
}






 5.在dao层下建立接口(BillDao)

package com.yhzz.dao;

import com.yhzz.pojo.Bill;

import java.util.List;

//查询
public interface Billdao {
    //获取总条数
    int queryBillCount();
//    起始索引=(当前页码数-1)*页面容量
    List<Bill> queryBillimit(int currentPageNo, int PageSize);

}

在dao层下创建impl,在impl下创建一个类,实现接口,建立连接

package com.yhzz.dao.impl;

import com.yhzz.dao.Billdao;
import com.yhzz.pojo.Bill;
import com.yhzz.util.BaseDaoUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BillDaoImpl implements Billdao {
    @Override
    public int queryBillCount() {
        //建立连接
        Connection conn = BaseDaoUtil.getConn();
        //获取Preparestatement对象
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        int count = 0;
        try {
            statement = conn.prepareStatement("select count(*) from smbms_bill");
            resultSet = statement.executeQuery();
            while (resultSet.next()){
                count = resultSet.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            BaseDaoUtil.closeAll(conn,statement,resultSet);
        }
        return count;
    }

    @Override
    public List<Bill> queryBillimit(int currentPageNo, int pageSize) {
        Connection conn = BaseDaoUtil.getConn();
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        String sql = "select * from smbms_bill limit ?,?";
        try {
            statement = conn.prepareStatement(sql);
            statement.setInt(1,(currentPageNo-1)*pageSize);
            statement.setInt(2,pageSize);
            resultSet = statement.executeQuery();
            List<Bill> billList = new ArrayList<>();
            while (resultSet.next()){
                Bill bill = new Bill();
                bill.setId(resultSet.getInt("id"));
                bill.setProduceName(resultSet.getString("productName"));
                bill.setProductUnit(resultSet.getString("productUnit"));
                bill.setTotalPrice(resultSet.getDouble("totalPrice"));
                billList.add(bill);
            }
            return billList;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            BaseDaoUtil.closeAll(conn,statement,resultSet);
        }

        return null;
    }
}

6. service下建立接口

package com.yhzz.service;

import com.yhzz.pojo.Bill;

import java.util.List;

public interface BillService {
    //获取总条数
    int queryBillCount();
    //    起始索引=(当前页码数-1)*页面容量
    List<Bill> queryBillimit(int currentPageNo, int PageSize);
}

在service下创建一个impl,在impl下创建一个类,用于返回接口里面得方法

package com.yhzz.service.impl;

import com.yhzz.dao.Billdao;
import com.yhzz.dao.impl.BillDaoImpl;
import com.yhzz.pojo.Bill;
import com.yhzz.service.BillService;

import java.util.List;

public class BillServiceImpl implements BillService {
    Billdao billDao = new BillDaoImpl();
    @Override
    public int queryBillCount() {

        return billDao.queryBillCount();
    }

    @Override
    public List<Bill> queryBillimit(int currentPageNo, int pageSize) {
        return billDao.queryBillimit(currentPageNo,pageSize);
    }
}

 

7.在pojo包下创建实体类

package com.yhzz.pojo;
//实体类
public class Bill {
    private int id;
    private String produceName;//货物名称
    private String productUnit;//单位
    private double totalPrice;//总价格

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getProduceName() {
        return produceName;
    }

    public void setProduceName(String produceName) {
        this.produceName = produceName;
    }

    public String getProductUnit() {
        return productUnit;
    }

    public void setProductUnit(String productUnit) {
        this.productUnit = productUnit;
    }

    public double getTotalPrice() {
        return totalPrice;
    }

    public void setTotalPrice(double totalPrice) {
        this.totalPrice = totalPrice;
    }
}

8.在pojo包下创建一个实体类(PageObj)

package com.yhzz.pojo;

public class PageObj {
    //页面容量 pageSize 总条数totalCount  总页数:totalPageCount 当前页码数:currentPageNo
    private int pageSize=3;
//    总条数totalCount
    private int totalCount=0;
//    总页数:totalPageCount
    private int totalPageCount=0;
//    当前页码数:currentPageNo
    private int currentPageNo=1;

    public int getPageSize() {
        return pageSize;
    }

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


    public int getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(int totalCount) {
        if(totalCount>0){
            this.totalCount = totalCount;
            this.totalPageCount = totalCount%pageSize==0?totalCount/pageSize:totalCount/pageSize+1;
        }
    }

    public int getTotalPageCount() {
        return totalPageCount;
    }


    public int getCurrentPageNo() {
        return currentPageNo;
    }

    public void setCurrentPageNo(int currentPageNo) {
        if (currentPageNo>0){
            this.currentPageNo = currentPageNo;
        }
    }
}

在servlet里面创建一个类,继承HttpServlet 

package com.yhzz.servlet;

import com.yhzz.pojo.Bill;
import com.yhzz.pojo.PageObj;
import com.yhzz.service.BillService;
import com.yhzz.service.impl.BillServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class BillServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String currentPageNo = req.getParameter("currentPageNo");
        int currPageNo = 1;
        if (currentPageNo!=null&&!"".equals(currentPageNo)){//jsp传来的参数判断非空
            currPageNo=Integer.parseInt(currentPageNo);
        }
        BillService billService = new BillServiceImpl();
        //获取数据总条数
        int count = billService.queryBillCount();


        //页面容量 pageSize 总条数totalCount  总页数:totalPageCount 当前页码数:currentPageNo
        PageObj pageObj = new PageObj();
        pageObj.setPageSize(5);
        pageObj.setTotalCount(count);
        pageObj.setCurrentPageNo(currPageNo);
        //分页查询数据
        List<Bill> billList = billService.queryBillimit(pageObj.getCurrentPageNo(), pageObj.getPageSize());

        req.setAttribute("billList",billList);
        //把当前页面数传给jsp
        req.setAttribute("currentPageNo",pageObj.getCurrentPageNo());
        //把总页数传给jsp
        req.setAttribute("totalPageCount",pageObj.getTotalPageCount());
        req.getRequestDispatcher("billList.jsp").forward(req,resp);
    }
}

9.创建jsp文件,设置欢迎页面

<%@page isELIgnored="false" language="java" contentType="text/html; charset=utf-8" %>
<html>
<body>
<a href="${pageContext.request.contextPath}/bill">//获取上下文路径实现页面跳转
    查看商品列表
</a>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<div>
    <table style="border: solid black">
        <tr>
            <td>商品编号</td>
            <td>商品名称</td>
            <td>商品单位</td>
            <td>总价</td>
        </tr>
        <c:forEach items="${billList}" var="bill">
            <tr>
                <td>${bill.id}</td>
                <td>${bill.produceName}</td>
                <td>${bill.productUnit}</td>
                <td>${bill.totalPrice}</td>
            </tr>
        </c:forEach>
    </table>
</div>
<div>
    <a href="${pageContext.request.contextPath}/bill?currentPageNo=1">首页</a>
    <c:if test="${currentPageNo<=1}">
        <a href="${pageContext.request.contextPath}/bill?currentPageNo=1">上一页</a>
    </c:if>
    <c:if test="${currentPageNo>1}">
        <a href="${pageContext.request.contextPath}/bill?currentPageNo=${currentPageNo-1}">上一页</a>
    </c:if>

    <c:if test="${currentPageNo>=totalPageCount}">
        <a href="${pageContext.request.contextPath}/bill?currentPageNo=${totalPageCount}">下一页</a>
    </c:if>
    <c:if test="${currentPageNo<totalPageCount}">
        <a href="${pageContext.request.contextPath}/bill?currentPageNo=${totalPageCount+1}">下一页</a>
    </c:if>
    <a href="${pageContext.request.contextPath}/bill?currentPageNo=${totalPageCount}">尾页</a>
</div>
</body>
</html>

10.配置tomact,运行 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值