天天看點

利用純JSP技術實作分頁效果

??椤墊????????璺?锛?

1.杩??ユ?版??搴?

2.?峰???昏?闆???

3.瀹?涔?姣?椤墊?劇ず??

4.璁$???婚〉??

5.褰???椤典??間??ュ??

6.纭?瀹?褰???椤電?????達?棣?椤典???锛?灏鵑〉涔???

7.纭?瀹?姣?椤電??绗?涓??¤?闆??ㄦ?版??琛ㄧ??浣?缃?

8.?ヨ??

9.寰????劇ず姣?椤電??淇℃??

?

涓???互?ヨ?㈠?????烘??淇℃??涓轟?锛??版??搴?????SSQLServer?版??搴?锛?灞??闆??椤墊??????

?版??搴?students锛??版??琛?tu锛?瀛?娈墊??锛?瀛???stu_id)??濮???(stu_name)???у??(stu_sex)??骞撮?(stu_age)

璇??浠g??濡?涓?:

<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>

<html>

<head>

?<title>绾?JSP瀹??闆????淇℃????椤?lt;/title>

</head>

<body>

<table align="center" cellpadding="3" cellspacing="1" bgcolor="#0099FF" width="700">

?<%

??int totalCount = 0;//瀹?涔??昏?闆?????int pageSize = 2;//瀹?涔?姣?椤墊?劇ず????int pageCount = 0;//瀹?涔??婚〉????int currentPage = 1;//瀹?涔?褰????劇ず椤???try{

???String sql = "select count(*) from stu";//瀹?涔??峰???昏?闆??扮??SQL?戒護

???Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//??杞介┍?????Connection con = DriverManager.getConnection("jdbc:odbc:sun","sa","");//杩??ユ?版??搴?

???Statement st = con.createStatement(1004,1007);//??寤烘?ц?SQL?戒護??瀵矽薄

???ResultSet rs = st.executeQuery(sql);//?峰??缁???

???boolean b = rs.next();//灏?娓告??绉誨?ㄥ?扮??涓?琛?

???if(b)

???{

????totalCount = rs.getInt(1);//濡??????鹼??峰???昏?闆???????if(totalCount>0)

????{

?????pageCount = totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1;//璁$???婚〉???????String sp2 = request.getParameter("sp");//?ユ?跺???椤典????????if(sp2!=null)

?????{

??????try{

???????currentPage = Integer.parseInt(sp2);//灏??ユ?剁???艱?缁?褰???椤???????}catch(NumberFormatException nfe){

???????currentPage = 1;

??????}

?????}

??? //纭?瀹?褰???椤電?????村?ㄩ?椤典???锛?灏鵑〉涔???

?????if(currentPage < 1)

?????{

??????currentPage = 1;

?????}

?????if(currentPage > pageCount)

?????{

??????currentPage = pageCount;

?????}

?????sql = "select * from stu";? //瀹?涔??ヨ?㈡??????SQL?戒護

?????rs = st.executeQuery(sql);//?ц??ヨ?㈡??浣?

?????int position = (currentPage-1)*pageSize+1;//纭?瀹?姣?椤電??绗?涓??¤?闆??ㄦ?版??琛ㄧ??浣?缃?

?????rs.absolute(position);//瀹?浣?

????//寰????劇ず

?????for(int i = 0 ; i < pageSize ; i ++)

?????{

?????//褰?娓告??娌℃???拌揪????涓?琛?涔????跺驚???劇ず

??????if(rs.isAfterLast()==false)

??????{

???????int id = rs.getInt(1);

???????String name = rs.getString(2);

???????String sex = rs.getString(3);

???????int age = rs.getInt(4);

?%>

???????<tr bgcolor="#FFFFFF">

????????<td><%=id%></td>

????????<td><%=name%></td>

????????<td><%=sex%></td>

????????<td><%=age%></td>

???????</tr>

?<%

???????rs.next();

??????}

?????}

?%>

??????<tr bgcolor="#FFFFFF">

???????<td colspan="4">

????????[<a href="?sp=1" target="_blank" rel="external nofollow" >棣?椤?lt;/a>]

????????[<a href="?sp=<%=currentPage-1%>" target="_blank" rel="external nofollow" >??椤?lt;/a>]

????????[<a href="?sp=<%=currentPage+1%>" target="_blank" rel="external nofollow" >??椤?lt;/a>]

????????[<a href="?sp=<%=pageCount%>" target="_blank" rel="external nofollow" >灏鵑〉</a>]

???????</td>

??????</tr>

?<%

????}

???}

??}catch(ClassNotFoundException cnfe){

???out.print(cnfe);

??}catch(SQLException se){

???out.print(se);

??}

?%>

</table>

</body>

</html>

?

?