最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
数据库记录分页显示的类
时间:2008-04-28 编辑:简简单单 来源:一聚教程网
忙了一下午,写了个分页显示的类,自我感觉写的还不错,所以就贴出来了,呵呵!!!
import java.io.*;
import java.sql.*;
import java.util.*;
public class GetPage...{
private int pageSize = 6; //每一页显示的记录数
private int pageCount = 0; //总的页数
private int currentPage = 1; //当前显示的页
//"showVar" 值为"1"表示显示第一页,为"2"表示显示上一页,为"3"表示显示下一页,为"4"表示显示最 后页,否则显示当前页。
public Vector<Vector> doGet(int showVar)...{
Vector<Vector> all = new Vector<Vector>(); //当前页的记录集
try ...{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); //定义数据库驱动
String url =
"jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=lesson"; //连接数据库"lesson"
String user = "sa"; //用户名
String password = "123"; //密码
Connection con = DriverManager.getConnection(url, user, password); //连接
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
String sql = "select * from upLoad"; //查询语句
ResultSet rs = stmt.executeQuery(sql);
rs.last();
int lastrow = rs.getRow(); //获取记录的总行数
pageCount = (lastrow % pageSize == 0) ? (lastrow / pageSize) : (lastrow / pageSize + 1);
if (pageCount == 0) ...{
pageCount = 1;
}
switch (showVar) ...{
case 1:
currentPage = 1;
break;
case 2:
currentPage = (currentPage > 1) ? (currentPage - 1) : 1;
break;
case 3:
currentPage = (currentPage < pageCount) ? (currentPage + 1) : pageCount;
break;
case 4:
currentPage = pageCount;
break;
default:
currentPage = currentPage;
}
int posion = (currentPage - 1) * pageSize + 1;
rs.absolute(posion);
rs.previous(); //定位指针
//****************获取当前页的数据*******************
Vector<Integer> id = new Vector<Integer>();
Vector<String> zhuti = new Vector<String>();
for (int i = 0; i < pageSize && rs.next(); i++) ...{
id.add(rs.getInt("id"));
zhuti.add(rs.getString("zhuti"));
}
all.add(id);
all.add(zhuti);
//*************************************************
rs.close();
stmt.close();
con.close();
}
catch (Exception ex) ...{
ex.printStackTrace();
}
return all;
}
public int getPageCount()...{
ruturn pageCount;
}
public int getCurrentPage()...{
ruturn currentPage;
}
}
<
相关文章
- SpringBoot测试配置属性与web启动环境解析 10-24
- vue中将el-switch值true、false改为number类型的1和0解析 10-24
- Vue中的路由配置项meta使用解读 10-24
- SpringBoot自定义bean绑定解析 10-24
- SpringBoot常用计量与bean属性校验和进制数据转换规则解析 10-24
- 工厂方法在Spring框架中的运用介绍 10-24
热门栏目
-
php教程
php入门 php安全 php安装 php常用代码 php高级应用 -
asp.net教程
基础入门 .Net开发 C语言 VB.Net语言 WebService -
手机开发
安卓教程 ios7教程 Windows Phone Windows Mobile 手机常见问题 -
css教程
CSS入门 常用代码 经典案例 样式布局 高级应用 -
网页制作
设计基础 Dreamweaver Frontpage js教程 XNL/XSLT -
办公数码
word excel powerpoint 金山WPS 电脑新手 -
jsp教程
Application与Applet J2EE/EJB/服务器 J2ME开发 Java基础 Java技巧及代码