- 论坛徽章:
- 0
|
%@ page contentType="text/html;charset=GB2312"%>
%@ page import="java.sql.*"%>
%
int dipage=1;//当前页码数默认为1
String pages=request.getParameter("dipage");
if(pages==null)
{
pages="1";
}
try {
dipage=Integer.parseInt(pages);
}
catch(Exception e)
{
dipage=1;
}
%>
html>
title>分页测试/title>
body>
%
Connection con;
Statement sql;
ResultSet rs;
try
{
Class.forName("net.sourceforge.jtds.jdbc.Driver"); //加载驱动
}
catch(ClassNotFoundException e)
{
out.print("类找不到!");
}
try {
con=DriverManager.getConnection("jdbc:jtds:sqlserver://localhost:1433/Northwind","sa","131289"); //创建数据库连接对象
//ResultSet.TYPE_SCROLL_SENSITIVE :结果集记录指针可以上下移动,数据库内容发生变化后结果记录集同步改变
//ResultSet.CONCUR_READ_ONLY :结果记录集只读,不能更新数据库表中的数据
sql=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY); //创建SQL语句对象
rs=sql.executeQuery("select EmployeeID,LastName,FirstName,Title,TitleOfCourtesy,City from Employees");
int countRecord=0;//记录条数
int countPageRecord=0;//每页记录条数
int countPage=0;//总页数
countPageRecord=4;//每页5条记录,要设置每页记录数就更改这个变量的值
//得到记录的条数
rs.last();
countRecord=rs.getRow();
//得到总页数
if (countRecord/countPageRecord==0)
countPage=countRecord/countPageRecord+1;
else if(countRecord == countPageRecord)
countPage=countRecord/countPageRecord;
else
countPage=countRecord/countPageRecord+1;
//把记录指针移至当前页第一条记录之前
if((dipage-1)*countPageRecord==0)
rs.beforeFirst();
else
rs.absolute((dipage-1)*countPageRecord);
out.print("");
out.print("");
out.print("");
out.print(""+"员工ID号");
out.print(""+"姓");
out.print(""+"名");
out.print(""+"职位");
out.print(""+"性别");
out.print(""+"城市");
out.print("");
int i=0;
while(rs.next())
{
out.print("");
out.print(""+rs.getInt("EmployeeID")+"");
out.print(""+rs.getString("LastName")+"");
out.print(""+rs.getString("FirstName")+"");
out.print(""+rs.getString("Title")+"");
out.print(""+rs.getString("TitleOfCourtesy")+"");
out.print(""+rs.getString("City")+"");
out.print("");
i++;
if (i>=countPageRecord) break; //当前页显示完,则退出循环
}
out.print("");
out.print("共"+countRecord+"条记录,共"+countPage+"页,当前第"+dipage+"页,每页"+countPageRecord+"条记录,");
if (dipage==1) //当前是首页
;
else //当前不是首页
{
out.print("首页,");
out.print("+(dipage-1)+">上一页,");
}
if(dipage==countPage) //当前是末页
;
else //当前不是末页
{
out.print("+(dipage+1)+">下一页,");
out.print("+countPage+">末页");
}
out.print("");
out.print("");
out.print("");
con.close();
}
catch(SQLException el)
{
out.print("SQL异常!");
}
%>
/body>
/html>
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/8872/showart_305716.html |
|