Jsp頁面分頁程式

來源:互聯網
上載者:User

<%@ page import="java.sql.*" %>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%! String au_id,au_lname,au_fname,phone,address,city,state;%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>ArticleList</TITLE>
<link rel="stylesheet" href="style.css">
</head>
<body bgcolor="#FFFFFF">
<table width="834" border="0" cellspacing="0" cellpadding="0" align="center">
<tr>
<td width="834">
<table width="800" border="0" cellspacing="2" cellpadding="2" align="center">
<tr align="center">
<td width="792">&nbsp;</td>
</tr>
<tr>
<td>
<table width="794" border="0" cellspacing="1" cellpadding="1" align="center">
<tr align="center" bgcolor="#CCCCCC">
<td><b>標題</b></td>
<td><b>作者</b></td>
<td><b>類型</b></td>
<td><b>點擊</b></td>
<td width="80"><b>加入時間</b></td>
</tr>
<%
int i;
int ipage=5; //分頁單位
int allpage; //總頁數
int pages; //接受的頁碼變數
int cpage=1; //當前頁
int spage; //開始頁
int allCol=0;
Vector v=null;
//資料庫連接聲明
Connection conn=null;
 java.lang.String sql,pagesql;
 java.sql.ResultSet rs,pagers;
 Statement statement=null;
 String kmsdb="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=db_szscz";
 Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
    conn = DriverManager.getConnection(kmsdb, "sa", "");
 
 pagesql = "SELECT COUNT(*) FROM  tb_news";
//取總文章數
statement = conn.createStatement();
pagers = statement.executeQuery(pagesql);
 while(pagers.next())
  {
  allCol = pagers.getInt(1);
  System.out.println(allCol);
  }
 
//獲得總頁面數
allpage = (int)Math.ceil((allCol + ipage-1) / ipage);
//判斷參數pages是否空
if (request.getParameter("pages")==null) {
pages=1;
} else {
pages = new Integer(request.getParameter("pages")).intValue();
System.out.println(pages);
}
//判斷當前頁
if(pages > allpage || pages == 0)
{cpage = 1;}
else
{
cpage = pages;
}
//判斷起始頁
sql = "select Top "+ipage+" news_id,news_title,news_author,news_content from tb_news where news_id not in (select top "+(cpage-1)*ipage+" news_id from tb_news order by news_id desc)order by news_id desc";
rs = statement.executeQuery(sql);
while (rs.next())
{
 au_id=rs.getString("news_id");
 au_lname=rs.getString("news_title");
 au_fname=rs.getString("news_author");
%>
 <td width="175"><%= au_id %></a></td>
 <td width="175"><%= au_lname %></a></td>
 <td width="175"><%= phone %></a></td>
 <td width="171"><%= address %></a></td>
</tr>
<%   
}%>
<tr bgcolor="#CCCCCC">
<td colspan="5" align="right" valign="middle">共 <%= allCol %>條記錄 共 <%= allpage %>頁 <% if (cpage>1) { %><a href="a.jsp?pages=<%= cpage-1 %>">PRE</a><% } if (cpage<allpage) { %><a href="a.jsp?pages=<%= cpage+1 %>">NEXT</a><% } %> / <% for (i=1;i<=allpage;i++) { if (i != pages) {%><a href="a.jsp?pages=<%= i %>"><%= i %></a>&nbsp;<% }} %></td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.