Method One
Submit to this page "dividepage.jsp"
<%@ page language= "java" contenttype= "text/html;charset=gb2312"%>
<%@ page import= "java.sql.*,java.lang.string,java.util.*"%>
<title>
Paging display
</title>
<%! String url,sql,xh; %>
<%! Connection conn;%>
<%! ResultSet rs;%>
<%! Statement stmt;%>
<%!int i;%>
<%!int pagesize=3;%><!--The number of record bars displayed per page is 3-->
<%!int rowcount=0;%>
<%!int pagecurrent;%><!--The current number of pages is "Pagecurrent"-->
<%!int pagecount;%><!--Total number of pages pagecount-->
<body>
<table width= "80%" border= "1" cellspacing= "1" cellpadding= "1" align= "Center" >
<tr>
<TD width= "16%" align= "Center" > School Number </td>
<TD width= "16%" align= "center" > Name </td>
<TD width= "8%" align= "center" > Gender </td>
<TD width= "8%" align= "center" > Age </td>
<TD width= "12%" align= "center" > Birthplace </td>
<TD width= "12%" align= "center" > Faculty </td>
<TD width= "20%" align= "center" > Change </td>
<TD width= "20%" align= "center" > Delete </td>
</tr>
<%
/**
The main purpose of the following database connection is to find the total number of records rowcount
*/
Try
{
Class.forName ("Sun.jdbc.odbc.JdbcOdbcDriver");
Url= "Jdbc:odbc:university";
Conn=drivermanager.getconnection (URL, "Guojia", "19861213");
Stmt=conn.createstatement ();
Sql= "SELECT * from student";
Rs=stmt.executequery (SQL);
Out.print (ROWCOUNT);
rowcount=0;//notice here the Qing 0 must not be less, otherwise you a little hyperlink, all the records are added again
while (Rs.next ())
{
rowcount++;
}
Rs.close ();
Stmt.close ();
Conn.close ();
}catch (Exception e)
{
Out.print (e);
}
%>
<%
try{
Class.forName ("Sun.jdbc.odbc.JdbcOdbcDriver");
Url= "Jdbc:odbc:university";
Conn=drivermanager.getconnection (URL, "Guojia", "19861213");
Stmt=conn.createstatement ();
Sql= "SELECT * from student";
Rs=stmt.executequery (SQL);
String strpage=request.getparameter ("page");//"page passes arguments for connection"
/**
Page must be null the first time the page is run, because the argument is not passed, the Pagecurrent value is set to 1
It means the first page.
*/
if (strpage==null)
{
Pagecurrent=1;
}
else{
Pagecurrent=integer.parseint (strpage);
if (pagecurrent<1)
Pagecurrent=1;
}
/**
The purpose of the following two sentences is to position the cursor to the last record on the previous page of the page to display
*/
For (i=1;i<= (pageCurrent-1) *pagesize;i++)
Rs.next ();
/**
The purpose of the following for loop is to place the page's record in the table
*/
for (i=1;i<=pagesize;i++)
{
if (Rs.next ())
{
Xh=rs.getstring (1);
%>
<tr>
<TD width= "16%" align= "Center" >
<%=xh%>
</td>
<TD width= "16%" align= "Center" >
<%=rs.getobject (2)%>
</td>
<TD width= "8%" align= "Center" >
<%=rs.getobject (3)%>
</td>
<TD width= "8%" align= "Center" >
<%=rs.getobject (4)%>
</td>
<TD width= "12%" align= "Center" >
<%=rs.getobject (5)%>
</td>
<TD width= "12%" align= "Center" >
<%=rs.getobject (6)%>
</td>
<TD width= "18%" align= "Center" >
<a href= "change.jsp?xuehao=<%=xh%>" > Modification </a>
</td>
<TD width= "18%" align= "Center" >
<a href= "del.jsp?xuehao=<%=xh%>" > Delete </a>
</td>
</tr>
<%
}
}
Out.print (Conn.getautocommit ())//This statement tests Conn Autocommit no, if not, add Conn.commit () after executeupdate ();
Rs.close ();
Stmt.close ();
Conn.close ();
pagecount= (int) (rowcount/pagesize);
if (Pagesize*pagecount<rowcount)
{
pagecount++;
}
Out.print ("A total of" +pagecount+ "page");
if (pagecount>1)
{
for (i=1;i<=pagecount;i++)
Out.print ("<a href= ' dividepage.jsp?page=" +i+ "' >" +i+ "</a>"); It's easy to make mistakes.
/**
Out.print ("<a href= ' dividepage.jsp?page= '" +i+ "' >" +i+ "</a>"); Here's the error on the page equals No "'"
The double quotes inside the double quotes become single quotes.
*/
}
Out.print ("At present is the first" +pagecurrent+ "page");
}
catch (Exception e)
{
Out.print (e);
}
%>
</table>
<div align= "center" >
<a href= "insert.jsp" > Add new Record </a></div>
</body>
Method Two
<%@ page language= "java" contenttype= "text/html;charset=gb2312"%>
<%@ page import= "java.sql.*,java.lang.string.*,java.util.*"%>
<title>
JSP to achieve page paging function
</title>
<%!
Connection Conn;
ResultSet rs;
Statement stmt;
String Sql,url,strpage,no;
int pagesize=3;//The number of records displayed on one page
int rowcount;//Total Records
int currentpage;//Current Display page
int pagecount;//Total Pages
int i;
%>
<%
Strpage=request.getparameter ("page");
if (strpage==null)
{
currentpage=1;
}
Else
{
Currentpage=integer.parseint (strpage);
if (currentpage<1)
{
currentpage=1;
}
}
%>
<%
try{
Class.forName ("Sun.jdbc.odbc.JdbcOdbcDriver");
Url= "Jdbc:odbc:university";
Conn=drivermanager.getconnection (URL, "Guo", "194871213");
Stmt=conn.createstatement (resultset.type_scroll_insensitive,resultset.concur_read_only);
Sql= "SELECT * from student";
Rs=stmt.executequery (SQL);
Rs.last ();
Rowcount=rs.getrow ();
Pagecount= (rowcount+pagesize-1)/pagesize;
if (Currentpage>pagecount)
{
Currentpage=pagecount;
}
}
catch (Exception e)
{
Out.print (e);
}
%>
<body>
<div align= "center" >
<font size= "6" color= "Blue" ><b>jsp paging feature </b></font>
</div>
<table border= "1" align= "center" cellpadding= "1" cellspacing= "1" >
<tr>
<TD width= "16%" align= "Center" > School Number </td>
<TD width= "16%" align= "center" > Name </td>
<TD width= "8%" align= "center" > Gender </td>
<TD width= "8%" align= "center" > Age </td>
<TD width= "12%" align= "center" > Birthplace </td>
<TD width= "12%" align= "center" > Faculty </td>
<TD width= "20%" align= "center" > Change </td>
<TD width= "20%" align= "center" > Delete </td>
</tr>
<%
if (pagecount>0)
{
Rs.absolute ((currentPage-1) *pagesize+1);
i=0;
while (I<pagesize &&!rs.isafterlast ())
{
No=rs.getstring ("Xuehao");
%>
<tr>
<TD width= "16%" align= "Center" >
<%=NO%>
</td>
<TD width= "16%" align= "Center" >
<%=rs.getobject (2)%>
</td>
<TD width= "8%" align= "Center" >
<%=rs.getobject (3)%>
</td>
<TD width= "8%" align= "Center" >
<%=rs.getobject (4)%>
</td>
<TD width= "12%" align= "Center" >
<%=rs.getobject (5)%>
</td>
<TD width= "12%" align= "Center" >
<%=rs.getobject (6)%>
</td>
<TD width= "18%" align= "Center" >
<a href= "change.jsp?xuehao=<%=no%>" > Modification </a>
</td>
<TD width= "18%" align= "Center" >
<a href= "del.jsp?xuehao=<%=no%>" > Delete </a>
</td>
</tr>
<%
Rs.next ();
i++;
}
}
Rs.close ();
Stmt.close ();
Conn.close ();
%>
</table><br>
<div align= "center" >
<%=currentPage%> page Total <%=pageCount%> page <%if (currentpage<pagecount) {%><a href= Seperatepage.jsp?page=<%=currentpage+1%> "> next page </a>
<%
}
if (currentpage>1) {%><a href= "seperatepage.jsp?page=<%=currentpage-1%>" > Prev </a>
<%
}
if (pagecount>0)
{
int n;
for (n=1;n<=pagecount;n++)
{
%>
<a href= "seperatepage.jsp?page=<%=n%>" > <%=n%> page </a>
<%
}
}
%>
</div>
</body>