我正在使用此代码一次获取1000条记录。它工作正常,但我如何停止在第一个1000运行我的工作,然后拿起我离开的地方,并获得下一组1001 - 2000,运行下一个工作,等等?请帮助我有点卡住。
public class PaginationJDBC {
public static void main(String arg) {
for(int i=1; i<=5 ;i++){
paginationMethod(i);
}
}
static void paginationMethod(int n){
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
con = gettingMyConnection;
ps = con.prepareStatement("select emp.id, emp.name "
+ "from ( select rownum rn, e.* from EMPLOYEE e) emp "
+ "where rn >=? and rn< =? ");
ps .setInt(1, (n*1000) -999);
ps .setInt(2, n*1000);
rs = prepStmt.executeQuery();
int rowCount = 0;
while (rs.next()) {
++rowCount;
System.out.print(rs.getInt(1)+" ");
System.out.println(rs.getString(2));
if(rowCount >= 1000-4) {
system.out.printlin("total records" + rowCount);
//I would like to run a job here, returns the 1st 1000
return;
}
if(rowCount >= 1001) {
**// this is where I would do the next job between 1001 and 2000 and so on, but I'm stuck**
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
finally{
try {
if(rs!=null) rs.close(); //close resultSet
if(ps !=null) ps .close(); //close PreparedStatement
if(con!=null) con.close(); // close connection
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
答案 0 :(得分:0)
使用您的应用程序记录您所处的位置,例如,假设我今天显示记录,一次显示20行。我可以写:
第1页
select *
from T
where date_col = trunc(sysdate)
order by id desc
fetch first 20 rows only
我将ID = 100提取到80 ...我注意到80.我的下一个查询将是
select *
from T
where date_col = trunc(sysdate)
AND ID<80 <<==== additional predicate
order by id desc
fetch first 20 rows only
我的下一个查询是
select *
from T
where date_col = trunc(sysdate)
AND ID<60
order by id desc
fetch first 20 rows only
等等。