分页显示数据库表中的记录

上传人:ni****g 文档编号:509404576 上传时间:2022-10-26 格式:DOCX 页数:23 大小:41.14KB
返回 下载 相关 举报
分页显示数据库表中的记录_第1页
第1页 / 共23页
分页显示数据库表中的记录_第2页
第2页 / 共23页
分页显示数据库表中的记录_第3页
第3页 / 共23页
分页显示数据库表中的记录_第4页
第4页 / 共23页
分页显示数据库表中的记录_第5页
第5页 / 共23页
点击查看更多>>
资源描述

《分页显示数据库表中的记录》由会员分享,可在线阅读,更多相关《分页显示数据库表中的记录(23页珍藏版)》请在金锄头文库上搜索。

1、利用以下技术来实现对Oracle数据库中的表Emp中的全部记录实行分 页显示。技术点:(JSP+Sei-vlet+JavaBean+c3pO+Oracle)1、com.sun.pojo 类代码:package com.sun.pojo;impoit j ava .io. Serializable;impoit java.util.Date;public class Emp implements Serializable private static final long serialVersionUID = IL;private Integer empno;/ 员 工编号private Str

2、ing ename;/员工姓名private String job;/ 工种private Integer mgr;/ 上级经理编号private Date hiredate;/ 入职日期private Double sal;/ 薪水private Double conmi;/ 津贝占private Integer deptno;/ 部门编号/ no op1public Integer getEmpno() return empno;ipublic void setEmpno(Integer empno) this.empno = empno;ipublic String getEname()

3、 return ename;ipublic void setEname(String ename) this.ename = ename;ipublic String getJob() retimi job;ipublic void setJob(String job) this.job = job;ipublic Integer getMgr() return mgr;ipublic void setMgr(Integer mgr) this, mgr = mgr;ipublic Date getHiredate() retiini liiredate;ipublic void setHir

4、edate(Date hiredate) this.hiredate = liiredate;ipublic Double getSal() retiini sal;ipublic void setSal(Double sal) this, sal = sal;ipublic Double getConmi() retiini coimn;ipublic void setConmi(Double coimn) this.conun = conmi;ireturn deptno;public void setDeptno(Integer deptno) this.deptno = deptno;

5、2、分页封装类package com.sun.page;import java.util.List;*分页封装类可以适用于任何一张表* author AdministrmtorSuppressWarnings(uncbecked”)public class Pager private static final int size = 5;/设置每页显示几彳亍privateinttotalpage;/设f总页数一即(表的记录数/行数)+1或者(表的记录数/size)private List list;/设置把分页后的数据放到此List集合* re turn返回每页显示几行*/public stat

6、ic int getSize ()return size;/犬犬犬* re turn返回的总页数*/public int getTotalpage()return totalpage;/犬犬*设置总页数一即(表的记录数/行数)+1或者(表的记录数/行数)犬* Sparam totalpage* 总页数/thistotalpage = totalpage;public List getList ()return list;/犬犬*设置把分页后的数据放到此List集合犬* param list*/public void setList(List list) this 1ist = list;3、d

7、ao 类“31连接数据库的工具类(使用了连接池)package com.sun.dao;impoit java.beans.Property VetoException;impoit j a va. sql .Connection;impoit java. sql. Statement;impoit java.sql.ResultSet;impoit j a va. sql. SQLException;impoit com.mchange.v2.c3pO.ComboPooledDataSource;public class DBUtil private static ComboPooledDa

8、taSource cpd;/ 创建 c3p0 连接池的数 据源对彖/使川单例模式创建连接池static String driverName = uoracle.jdbc.driver.OracleDriver;String url = njdbc:oracle:thin:127.0.0.1:1521 :orcln;String user = scott*;String password = tiger;cpd = new ComboPooledDataSource();/使用连接池连接数据库cpd.setDriverClass(driverName); cpd.setJdbcUrl(url);

9、cpd.setUser(user);cpd.setPassword(password);/设置连接池管理属性 cpd.setMaxPoolSize(lO);/设置连接池中最大连接数冃 cpd.setInitialPoolSize(5);/设置连接池中初始化连接数冃 cpd.setAcquirelncrement(3);/ 自动增长数目cpd.setMaxStatements(50);/最大执行sql语句(一次可以执行 多少条sql语句) catch (PropertyVetoException pve) pve .printStackTraceQ;/*获得数据库的连接retiini Coime

10、ction 对彖public static Coimection getCoimection() Coimection coim = null;coim = cpd.getComiection();/从c3p0连接池获得数据库连接 catch (SQLException sqle) sqle.printStackTrace();retiini coim;*关闭数据库连接* param coim* Comiection 对彖* param ps* Statement 对象* param rs* ResultSet 对彖public static void close(Connection con

11、n, Statement ps, ResultSet rs) if (rs != null) rs.close();1Jif (ps != null) ps.close();1Jif (comi != null) coim.close(); catch (SQLException e) e.printStackTrace();1)1J32-表的操作类package com.sun.dao;impoit java.sql.Comiection; impoit j ava .sql. Prepared Statement; impoit java.sql.ResultSet;impoit java

12、.sql.SQLException;impoit java.util. AiiayList;impoit java.util.List;impoit com.newer.page.Pager; impoit com.liewer.poj o.Emp;*把emp表按照固定的行数分页* author Administrator*/public class EmpDao * param pageno* 表示查询emp表中的第几页* renini返回一个List集合,集合中存放的是emp表中的第几 页的记录*/public List find(int pageno) Coimection comi =

13、 null;Prepared Statement ps = null;ResultSet rs = null;List list = new ArrayList();coim = DBUtil.getCoimection();String sql = select * from (select row_nuinber() over (order by empno) ni,emp.* from emp) where rn? and 门i=?”;此页面开始行数的位置(不含)/Pager.getSize()表示 每一页要显示的行数。pageno表示第 几页int begin = Pager.getS

14、ize() * (pageno 1);此页面结束行数的位置(含)int end = Pager.getSize()*pageno ;System.out.pnntln(n从第 ” + (begin+1) + ” 条到第” + end + ” 条数据”);ps = conn.prepareStatement(sql);ps.setlnt(l, begin);ps.setlnt(2, end);rs = p s. execu teQn eiy ();while (rs.next() / 循环结果集。/从结果集中获得一条(行)记录,且封装到POJO类emp Emp emp = new Emp();emp. setEmpiio (rs. ge tlnt( ” einpiio ”); emp.setEname(rs.getStnng(nenameM); emp.setJob(rs.getString(njobn); emp.setMgr(rs.getInt(nmgrn); emp.setHiredate(rs.getDate(nhiredaten);emp. setSal(rs. getDouble(n sal11); emp.setConun(rs.getDouble(n

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 学术论文 > 其它学术论文

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号