在java jdbc

时间:2017-01-31 23:36:48

标签: java jdbc pagination apache-drill

我正在尝试使用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;
}

2 个答案:

答案 0 :(得分:0)

您可以编写如下的包装器:

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"));  

答案 1 :(得分:0)

首先,您必须获得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);
    }
}

希望有所帮助。