高效简单的jsp分页
<%@ page import="java.sql.*,java.io.*,java.util.*" %>
<%@ page language="java" pageencoding="big5" %>
<%! 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="760" border="0" cellspacing="0" cellpadding="0" align="center">
<tr>
<td>
<table width="750" border="0" cellspacing="2" cellpadding="2" align="center">
<tr align="center">
<td> </td>
</tr>
<tr>
<td>
<table width="740" 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><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://127.0.0.1:1433;databasename=dbname";
class.forname("com.microsoft.jdbc.sqlserver.sqlserverdriver");
conn = drivermanager.getconnection(kmsdb, "user", "pwd");
pagesql = "select count(*) from authors";
//取总文章数
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+" au_id,au_lname,au_fname,address,phone from authors where au_id not in (select top "+(cpage-1)*ipage+" au_id from authors order by au_id desc)order by au_id desc";
rs = statement.executequery(sql);
while (rs.next())
{
au_id=rs.getstring("au_id");
au_lname=rs.getstring("au_lname");
au_fname=rs.getstring("au_fname");
phone=rs.getstring("phone");
address=rs.getstring("address");
%>
<td width="300"><%= au_id %></a></td>
<td width="300"><%= au_lname %></a></td>
<td width="300"><%= au_fname %></a></td>
<td width="300"><%= phone %></a></td>
<td width="300"><%= address %></a></td>
上一篇: jsp生成html--replace