I have implemented my idea of pagination and came up with a solution. But the problem is, When I execute this query in my Oracle APEX directly, The results are perfect for second page, when I do the same using preparedstatement and display it in the jsp page, It combines the page one and page two result in page no 2. Here is the code :
public List<Course> selectAllCourse(int pgno) {
List<Course> allcourses = new ArrayList<>();
try {
connection();
PreparedStatement count = con.prepareStatement("SELECT COUNT(*) FROM COURSE");
ResultSet cou=count.executeQuery();
cou.next();
double max=cou.getDouble(1);
max=max/5;
max=Math.ceil(max);
int top=(int)max;
System.out.println(top);
PreparedStatement preparedStatement = con.prepareStatement("select * from (select * from course where rownum<? minus select * from course where rownum<?) order by COURSEID asc");
int first=1+(pgno-1)*5;
int limit=1+(pgno)*5;
System.out.println();
preparedStatement.setInt(2, first);
preparedStatement.setInt(1, limit);;
System.out.println(preparedStatement);
ResultSet rs = preparedStatement.executeQuery();
while (rs.next()) {
String title=rs.getString("title");
String detail=rs.getString("detail");
int sessions=rs.getInt("sessions");
int author=rs.getInt("author");
String status=rs.getString("status");
String doc=rs.getString("dateofcreation");
int hours=rs.getInt("hours");
int price=rs.getInt("price");
int courseid=rs.getInt("courseid");
allcourses.add(new Course(courseid,title, detail, sessions, author, status, doc, hours, price));
}
} catch (SQLException e) {
e.printStackTrace();
} catch (ServletException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return allcourses;
}
Expected result in Page 1 and Page 2(Query Ran in APEX)
You can see that the page 2 in web has complete list including the page 1 rows too. I'm unsure why it is happening, Could this be because of multiple nested queries running in one prepared statement? Any help is appreciated. And I'm sorry if I'm not clear with my question.