【发布时间】: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