Java JDBC分页Oracle一次获取1000执行然后获取下一个1000
我用这个代码一次获取1000条记录。它工作得很好,但是我如何在第一个1000点停止运行我的作业,然后从我停止的地方开始运行下一组1001-2000,运行下一个作业,依此类推?请帮帮我,我有点困了Java JDBC分页Oracle一次获取1000执行然后获取下一个1000,java,oracle,jdbc,pagination,jdbc-odbc,Java,Oracle,Jdbc,Pagination,Jdbc Odbc,我用这个代码一次获取1000条记录。它工作得很好,但是我如何在第一个1000点停止运行我的作业,然后从我停止的地方开始运行下一组1001-2000,运行下一个作业,依此类推?请帮帮我,我有点困了 public class PaginationJDBC { public static void main(String arg) { for(int i=1; i<=5 ;i++){ paginationMethod(i); } } st
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。我将尝试setFetchSize方法。谢谢