【问题标题】:Implement pagination using offset in java jdbc在 java jdbc 中使用偏移量实现分页
【发布时间】:2017-06-17 14:11:40
【问题描述】:

我正在尝试使用 java jdbc 实现分页。我的查询采用批量大小和偏移量的限制。

如何在每批完成后增加偏移量。我无法理解如何使用此偏移量来实现功能。我试图编写一个包装器方法,它递归地调用获取偏移量的方法。

这是我的查询

select
ECId,InvId,ApplianceId,ProgName,CollectStartTS,CollectEndTS,CRPartyId,
HostName,IPAddess,SoftwareVer,OsType,collectserialnum,
CollectProductId,SNMPLoc,HWAlertCnt,SWAlertCnt,UploadProcEndTS
from inventory_details

和代码

public void getInventory() throws SQLException {
    int batch = Integer.parseInt(PropertyFileReader.getInstance()
         .getProperty("BATCHSIZE"));
    int offset= 0;
    while(offset<batch){
        DrillDAO dao = new DrillDAO();
        dao.getAllInventoryDetails(offset); 
    }   
    offset = offset+batch+1;
}

public ArrayList<InventoryDetail> getAllInventoryDetails(int offset)
             throws SQLException {              
    int batchSize = Integer.parseInt(PropertyFileReader.getInstance()
                    .getProperty("BATCHSIZE"));             

    ArrayList<InventoryDetail> inventoryDetailList = new ArrayList<InventoryDetail>();              

    Connection connection = null;
    Statement statement = null;
    ResultSet resultset = null;
    try {
        Class.forName(getJdbcDriver());
        connection = DriverManager.getConnection(
                     getJdbcURL(), getUserName(), getPassword());
        statement = connection.createStatement();
        LOGGER.info("Start time    " + CPUUtils.getCpuTime());
        resultset = statement.executeQuery(getQuery() 
                  + " " + "LIMIT" + " " + batchSize + " " 
                  + "OFFSET" + " " + offset);
        LOGGER.info("End time    " + CPUUtils.getCpuTime());

        while (resultset.next()) {
            InventoryDetail id = new InventoryDetail();
            id.setEcId(resultset.getString(1));
            id.setInvId(resultset.getString(2));
            id.setApplianceId(resultset.getString(3));
            id.setProgName(resultset.getString(4));
            id.setCollectStartTS(resultset.getString(5));
            id.setCollectEndTS(resultset.getString(6));
            id.setCrPartyId(resultset.getString(7));
            id.setHostName(resultset.getString(8));
            id.setIpAddess(resultset.getString(9));
            id.setSoftwareVer(resultset.getString(10));
            id.setOsType(resultset.getString(11));
            id.setCollectSerialNum(resultset.getString(12));
            id.setCollectProductId(resultset.getString(13));
            id.setSnmpLoc(resultset.getString(14));
            id.setHwAlertCnt(resultset.getString(15));
            id.setSwAlertCnt(resultset.getString(16));
            id.setUploadProcEndTS(resultset.getString(17));
            inventoryDetailList.add(id);
        }
    } catch (SQLException se) {
        se.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (statement != null)
                statement.close();
        } catch (SQLException se2) {
        }
        try {
            if (connection != null)
                connection.close();
        } catch (SQLException se) {
           se.printStackTrace();
        }
    }  
    return inventoryDetailList;
}

【问题讨论】:

  • 如果您没有为结果集指定某种排序方式,即在查询中声明 ORDER BY,则分页没有意义。行应该如何排序?

标签: java jdbc pagination apache-drill


【解决方案1】:

首先,您必须获取 SELECT 的计数:

String countQuery = "SELECT COUNT(0)\n" +
    "FROM INVENTORY_DETAILS";

现在您有了结果大小,询问它是否大于 0,这样您就不必执行原始查询:

// here goes the statement declaration and execution
long count = statement.execute(countQuery);
List<InventoryDetail> inventoryDetailList = new ArrayList<InventoryDetail>();

if (count > 0) {
    String query = "SELECT ECID,INVID,APPLIANCEID,PROGNAME,COLLECTSTARTTS,COLLECTENDTS,CRPARTYID,\n" +
        "    HOSTNAME,IPADDESS,SOFTWAREVER,OSTYPE,COLLECTSERIALNUM,\n" +
        "    COLLECTPRODUCTID,SNMPLOC,HWALERTCNT,SWALERTCNT,UPLOADPROCENDTS\n"+
        "FROM INVENTORY_DETAILS\n";

最后,这是指令中最重要的部分,您将遍历分页:

    long page = 1;
    long limit = page * offset;
    long loops = (count - (count % limit)) / offset;

    for (; page <= loops; page++) {
        if (page == 1) {
            query += "LIMIT " + limit;
        } else {
            limit = page * offset;
            query += "LIMIT " + limit + " OFFSET " + offset;
        }

        // here goes the statement declaration and execution
        statement.execute(query);
        // here goes the result set iteration and then...
        inventoryDetailList.add(id);
    }
}

希望对您有所帮助。

【讨论】:

  • 因为查询中没有指定行顺序,所以不能保证同一行不会出现在多个页面上。
  • 是的,我知道,但是实现之王需要更多的解释和更复杂的添加,因为它需要根据添加到行排序中的某些字段动态分页。我在这里的意图只是提示如何实现简单的分页。任何人都可以从 Spring 项目中查看这个 implementation,它解释了我之前的评论。
【解决方案2】:

你可以像下面这样编写包装器:

public List<InventoryDetail> getInventory() throws SQLException {
        int batch = Integer.parseInt(PropertyFileReader.getInstance().getProperty("BATCHSIZE"));
        int offset= 0;

        List<InventoryDetail> finalList = new ArrayList<InventoryDetail>();

        DrillDAO dao = new DrillDAO();
        for(int i = offset; ; i += batch+1) {
            //request data with one more, if exact that count return, you will know more data exist
            //so you can continue; when less than that return, you will know no more data, so break the loop
            ArrayList<InventoryDetail> invList = dao.getAllInventoryDetails(i, batch + 1);            
            if (invList.size() <= batch) {                
                break;
            } else {
                //remove the extra one called cz it will get in next call
                invList.remove(invList.size() - 1);                
            }
            finalList.addAll(invList);
        }

        return finalList;
    }

改变

public ArrayList<InventoryDetail> getAllInventoryDetails(int offset)
            throws SQLException {

public ArrayList<InventoryDetail> getAllInventoryDetails(int offset, int batchSize)
            throws SQLException {

并删除其中的以下行。

int batchSize = Integer.parseInt(PropertyFileReader.getInstance()
                    .getProperty("BATCHSIZE"));  

【讨论】:

    猜你喜欢
    • 2022-08-19
    • 1970-01-01
    • 2013-08-24
    • 2011-12-07
    • 2012-07-13
    • 2020-08-03
    • 1970-01-01
    • 2021-10-14
    • 2019-01-18
    相关资源
    最近更新 更多