JDBC 分页 Oracle 一次获取 1000,然后执行下一个 1000
JDBC Pagination Oracle get 1000 at a time execute then get next 1000
我正在使用此代码一次获取 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();
}
}
}
}
使用您的应用程序记下您到达的位置,例如,假设我要显示今天的记录,一次显示 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
等等。
我正在使用此代码一次获取 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();
}
}
}
}
使用您的应用程序记下您到达的位置,例如,假设我要显示今天的记录,一次显示 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
等等。