Efficient jsp paging query
Author:Eve Cole
Update Time:2009-07-02 17:22:40
Jsp is as follows:
**********************
<%@ page language="java" import="java.util.*,java.sql.*" %>
<%@ page contentType="text/html;charset=gb2312"%>
<jsp:useBean id="cn" scope="page" class="myConnection.Conn" /><!--Refer to the bean for database operations, complete it yourself, I will not go into details here-->
<%
int curpage=1;//Current page
int page_record=20;//The number of records displayed on each page
//Use the following method (sql query completed, fast)
curpage=Integer.parseInt(request.getParameter("page"));//Get the passed value and the page that needs to be displayed
ResultSet rs=cn.rsexecuteQuery("select top "+page_record+" * from tablename where id not in (select top "+(curpage*page_record)+" id from tablename order by id desc) order by id desc");
//This query statement obtains 20 records of the 1000 pages to be displayed. The general idea is-the subquery excludes all records before the records that need to be displayed, and the parent query sorts the remaining records in descending order.
while(rs.next) {
out.println(rs.getInt("id").toString());
}
rs.close();
%>