One, scrollable result set
Connection con = drivermanager.getconnection ();
PreparedStatement stmt = con.preparestatement (sql,resultset.type_forward_only,resultset.concur_read_only);
ResultSet rs = Stmt.executequery ();
Common methods:
(1) rs.absolute (n); You can jump the pointer to the nth row.
(2) rs.relative (n); You can move the pointer relative down or up n rows.
(3) Rs.first ();
(4) Rs.last ();
(5) int currow = Rs.getrow (); When the pointer is pointing forward
Second, the function of the decomposition of the idea is that if you want to display that page, you have to figure out the first record of each page is the record in all records, assuming that the first record of each page is the position record in the total record, then position= (ShowPage-1) x Pagesize+1. For example, if you want to display the first page, you should calculate the first record in the first page is the first record in the total record, if you want to display the second page, it is necessary to calculate the second page of the first record is the fourth record in the total record, if you want to display the third page, It is necessary to calculate that the first record in the first page is the Nineth record in the total record. 1. Number of calculated results
Rs.last ();
int size = Rs.getrow ();
You can get the number of results.
2. Need to divide several pages
If 5 records can be placed on a page, the
int pagecount = (size%5==0)? (SIZE/5):(size/5+1);
You will need to divide several pages to get it.
3. Control the number of records displayed on a page
If you can display 5 records on a page, you can count by using count.
int count = 0;
bo=
if (count>=5) break;
.....
count++;
}while (Rs.next ());
The break statement allows it to appear beyond the specified entry to jump out.
4. How to know the current page
With the feature of HTTP GET, the current address is indicated in the address bar, such as http://.......?curPage=1, which is now the first page.
String tmp = Request.getparameter ("Curpage");
if (tmp==null) {
tmp= "1";
}
Curpage = Integer.parseint (TMP);
The current page can be obtained.
Attention:
Rs.absolute (1); Indicates a point to the first record;
does not exist Rs.absolute (0);
Rs.absolute ((curPage-1) *pagesize+1); Adjusts the result set pointer to the beginning of the record that the current page should display.
For example, if a page shows 5 records, the current page is the second page, you need to adjust the pointer to 6, the current page is the third page, you need to adjust the pointer to 11.
5. Click on Home, Previous, next, last page behavior
<a href= "Multipage.jsp?curpage=<%curpage+1%>" > Next </a>
<a href= "multipage.jsp?curpage=<%curpage-1%>" > Prev </a>
<a href= "multipage.jsp?curpage=<%pagecount%>" > Last </a>
<a href= "multipage.jsp?curpage=1" > Home </a>
6. In order to save the current page position, you need to set the current page position as a global variable. Comprehensive Code:
[HTML]View Plaincopy
- <%@ page contenttype="text/html" pageencoding="GB2312" language="java"%>
- <%@ page import="java.sql.*"%>
- <html>
- <head>
- <title>hello</title>
- </head>
- <body>
- <table border="1" spacing= "2">
- <%!
- public static final String DRIVER = "Com.mysql.jdbc.Driver";
- public static final String USER = "root";
- public static final String PASS = "12345";
- public static final String URL = "JDBC:MYSQL://LOCALHOST:3306/MLDN";
- public static final int PAGESIZE = 5;
- int PageCount;
- int curpage = 1;
- %>
- <%
- 5 on one page
- String user = null;
- String pass = null;
- try{
- Class.forName (DRIVER);
- Connection con = drivermanager.getconnection (url,user,pass);
- String sql = "Select Empno,ename,job,hiredate,sal,comm from emp";
- PreparedStatement stat = con.preparestatement (sql,resultset.type_forward_only,resultset.concur_read_only) ;
- ResultSet rs = stat.executequery ();
- Rs.last ();
- int size = rs.getrow ();
- PageCount = (size%pagesize==0)? ( size/pagesize):(size/pagesize+1);
- String tmp = request.getparameter ("Curpage");
- if (tmp==null) {
- tmp="1";
- }
- curpage = integer.parseint (TMP);
- if (curpage>=pagecount) curpage = PageCount;
- Boolean flag = Rs.absolute ((curPage-1) *pagesize+1);
- Out.println (Curpage);
- int count = 0;
- bo=
- if (count>=pagesize) break;
- int empno = rs.getint (1);
- String ename = rs.getstring (2);
- String job = rs.getstring (3);
- Date hiredate = rs.getdate (4);
- float sal = rs.getfloat (5);
- int comm = Rs.getint (6);
- count++;
- %>
- <tr>
- <TD><%=empno%></td>
- <TD><%=ename%></td>
- <TD><%=job%></td>
- <TD><%=hiredate%></td>
- <TD><%=sal%></td>
- <TD><%=comm%></td>
- </tr>
- <%
- }while (Rs.next ());
- Con.close ();
- }
- catch (Exception e) {
- }
- %>
- </table>
- <a href = "multipage.jsp?curpage=1" > Home </a>
- <a href = "multipage.jsp?curpage=<%=curpage-1%>" > previous </a>
- <a href = "multipage.jsp?curpage=<%=curpage+1%>" > Next </a>
- <a href = "multipage.jsp?curpage=<%=pagecount%>" > Last </a>
- Section <%=curpage%> Page/Total <%=pagecount%> page
- </body>
- </html>
The idea is, if you want to show that page, you have to figure out the first record of each page is the number of records in all records, assuming that the first record per page is the position record in the total record, then position= (ShowPage-1) xpagesize+1. For example, if you want to display the first page, you should calculate the first record in the first page is the first record in the total record, if you want to display the second page, it is necessary to calculate the second page of the first record is the fourth record in the total record, if you want to display the third page, It is necessary to calculate that the first record in the first page is the Nineth record in the total record.
The core code in the JSP is as follows (the database used for MySQL):
Copy CodeThe code is as follows:
<%! int pagesize=4;
int PageCount;
int showpage;
%>
<!--connect the database and fetch records from the database--
<%
Connection con;
Statement SQL;
ResultSet rs;
Try{class.forname ("Com.mysql.jdbc.Driver");
}catch (ClassNotFoundException e) {
}
Try{con=drivermanager.getconnection ("Jdbc:mysql://localhost:3306/message board", "root", "123456");
Sql=con.createstatement (resultset.type_scroll_sensitive,resultset.concur_read_only);
Returning a scrollable result set
Rs=sql.executequery ("SELECT * from Messageinfo");
Move the cursor to the last row
Rs.last ();
Gets the line number of the last row
int Recordcount=rs.getrow ();
Calculate the total number of pages after paging
Pagecount= (recordcount%pagesize==0)? (lastrow/pagesize):(lastrow/pagesize+1);
Get the number of pages a user wants to display:
String integer=request.getparameter ("ShowPage");
if (integer==null) {
integer= "1";
}
Try{showpage=integer.parseint (Integer);
}catch (NumberFormatException e) {
showpage=1;
}
if (showpage<=1) {
showpage=1;
}
if (Showpage>=pagecount) {
Showpage=pagecount;
}
If you want to display page showpage, then the value of the position to which the cursor should be moved is:
int position= (showPage-1) *pagesize+1;
Set the position of the cursor
Rs.absolute (position);
Use for loop to display the records that should be displayed on this page
for (int i=1;i<=pagesize;i++) {
%>
<table>
<tr>
<th><%=rs.getstring ("UserName")%></th>
<td> Posted in: <%=rs.getstring ("datetime")%></td>
</tr>
<tr >
<th colspan= "3" ><textarea><%=rs.getstring ("content")%></textarea></th>
</tr>
</table>
<%
Rs.next ();
}
Rs.close ();
Con.close ();
}
catch (Exception e) {
E.printstacktrace ();}
%>
<br>
Page <%=showpage%> (total <%=pagecount%> page)
<br>
<a href= "showmessages.jsp?showpage=1" > Home </a>
<a href= "showmessages.jsp?showpage=<%=showpage-1%>" > Prev </a>
<%//Display the number of each page according to the value of PageCount and append the corresponding hyperlink
for (int i=1;i<=pagecount;i++) {
%>
<a href= "Showmessages.jsp?showpage=<%=i%>" ><%=i%></a>
<%}
%>
<a href= "Showmessages.jsp?showpage=<%=showpage+1%>" > Next </a>
<a href= "showmessages.jsp?showpage=<%=pagecount%>" > End </a>
<!--submit the number of pages the user wants to display through the form-
<form action= "" method= "get" >
Jump to section <input type= "text" name= "ShowPage" size= "4" > page
<input type= "Submit" name= "submit" value= "Jump" >
</form>
The results of the operation are as follows (to simplify the code, the page layout-related code is removed, this refers only to the function):
JSP paging technology implementation (using the current page to add and subtract, and use the href to carry out the current page value, the value of course is the current value variable)