JSP implementation page, paging function __jsp

Source: Internet
Author: User
Tags odbc rowcount stmt

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>
&LT;TD width= "16%" align= "Center" > School Number </td>
&LT;TD width= "16%" align= "center" > Name </td>
&LT;TD width= "8%" align= "center" > Gender </td>
&LT;TD width= "8%" align= "center" > Age </td>
&LT;TD width= "12%" align= "center" > Birthplace </td>
&LT;TD width= "12%" align= "center" > Faculty </td>
&LT;TD width= "20%" align= "center" > Change </td>
&LT;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>
&LT;TD width= "16%" align= "Center" >
<%=xh%>
</td>
&LT;TD width= "16%" align= "Center" >
<%=rs.getobject (2)%>
</td>
&LT;TD width= "8%" align= "Center" >
<%=rs.getobject (3)%>
</td>
&LT;TD width= "8%" align= "Center" >
<%=rs.getobject (4)%>
</td>
&LT;TD width= "12%" align= "Center" >
<%=rs.getobject (5)%>
</td>
&LT;TD width= "12%" align= "Center" >
<%=rs.getobject (6)%>
</td>
&LT;TD width= "18%" align= "Center" >
<a href= "change.jsp?xuehao=<%=xh%>" > Modification </a>
</td>
&LT;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>
&LT;TD width= "16%" align= "Center" > School Number </td>
&LT;TD width= "16%" align= "center" > Name </td>
&LT;TD width= "8%" align= "center" > Gender </td>
&LT;TD width= "8%" align= "center" > Age </td>
&LT;TD width= "12%" align= "center" > Birthplace </td>
&LT;TD width= "12%" align= "center" > Faculty </td>
&LT;TD width= "20%" align= "center" > Change </td>
&LT;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>
&LT;TD width= "16%" align= "Center" >
<%=NO%>
</td>
&LT;TD width= "16%" align= "Center" >
<%=rs.getobject (2)%>
</td>
&LT;TD width= "8%" align= "Center" >
<%=rs.getobject (3)%>
</td>
&LT;TD width= "8%" align= "Center" >
<%=rs.getobject (4)%>
</td>
&LT;TD width= "12%" align= "Center" >
<%=rs.getobject (5)%>
</td>
&LT;TD width= "12%" align= "Center" >
<%=rs.getobject (6)%>
</td>
&LT;TD width= "18%" align= "Center" >
<a href= "change.jsp?xuehao=<%=no%>" > Modification </a>
</td>
&LT;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%>" >&nbsp;<%=n%> page </a>
<%
}
}
%>
</div>
</body>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.