Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 848|回复: 0

Jdbc 实现分页 显示

[复制链接]

该用户从未签到

发表于 2011-8-2 21:42:23 | 显示全部楼层 |阅读模式
jdbc 实现分页,的实现
原理这个就不介绍了。。
总之是用jdbc 的游标移动
  1. package com.sp.person.sql.util;
  2. import java.sql.Connection;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.util.Map;
  7. import java.util.TreeMap;
  8. import javax.sql.DataSource;
  9. /**
  10. * 常常有同事在问JDBC 分页查询 这里给二个例子介绍一下
  11. * JDBC 分页查询
  12. * 分成二种方式希望对大家有所帮助
  13. * 分另表示了absolute 与relative 的区别
  14. * @see 这时用到一个数据源设计模式
  15. * 数据源与数据库连接没有关系
  16. * 例用接口回调的特性
  17. * @author liuqing
  18. * @version 1.0
  19. * 2011-8-2
  20. *
  21. */
  22. public class JdbcUtil {
  23. /**
  24.   * 数据源
  25.   */
  26. private DataSource dataSource;
  27. /**
  28.   * 不否启用多数据源
  29.   */
  30. private boolean isMultipleDataSource;
  31. /**
  32.   * 有时一个系统可能要使用多个数据源,存放多个数据源
  33.   */
  34. private Map<String,DataSource> dataSources = new TreeMap<String,DataSource>();
  35. /**
  36.   * if true isMultipleDataSource is Key to DataSource
  37.   */
  38. private String dataSourceKey;
  39. /**
  40.   * 默认构造器
  41.   */
  42. public JdbcUtil() {
  43.   
  44. }
  45. /**
  46.   * 构造器 Spring 的构造器注入
  47.   * @param dataSource
  48.   */
  49. public JdbcUtil(DataSource dataSource) {
  50.   this.dataSource = dataSource;
  51. }
  52. /**
  53.   * JDBC 分页查询
  54.   * @param sql       SQL 查询语句
  55.   * @param firstSize 起始页
  56.   * @param maxSize   返回数据条数
  57.   * @return ResultSet
  58.   * @throws SQLException
  59.   */
  60. public ResultSet queryPageAbsolute(String sql,
  61.    int firstSize,int maxSize) throws SQLException {
  62.   PreparedStatement pre = this.getConn().prepareStatement(sql);
  63.   pre.setMaxRows(maxSize);
  64.   ResultSet rs = pre.executeQuery();
  65.   rs.absolute(firstSize * maxSize);
  66.   return rs;
  67. }
  68. /**
  69.   * JDBC 分页查询
  70.   * @param sql        SQL 查询语句
  71.   * @param firstSize  起始页
  72.   * @param maxSize    返回数据条数
  73.   * @return ResultSet 返回结果集
  74.   * @throws SQLException
  75.   */
  76. public ResultSet queryPageRelative(String sql,
  77.    int firstSize,int maxSize) throws SQLException {
  78.   PreparedStatement pre = getConn().prepareStatement(sql);
  79.   pre.setMaxRows(maxSize);
  80.   ResultSet rs = pre.executeQuery();
  81.   rs.relative(firstSize);
  82.   return rs;
  83. }
  84. /**
  85.   *
  86.   * @return Connection
  87.   * @throws SQLException
  88.   */
  89. private Connection getConn() throws SQLException {
  90.   //使用多数据源的情况
  91.   if (this.isMultipleDataSource) {
  92.    DataSource v_dataSource = this.queryDataSourceByKey();
  93.    if (v_dataSource != null) {
  94.     return v_dataSource.getConnection();
  95.    }
  96.   }
  97.   return this.dataSource.getConnection();
  98. }
  99. /**
  100.   * 获得多数据源方法
  101.   * @return DataSource
  102.   */
  103. public DataSource queryDataSourceByKey() {
  104.   for (Map.Entry<String, DataSource> ds:this.dataSources.entrySet()) {
  105.    if (ds.getKey().equals(dataSourceKey)) {
  106.     return ds.getValue();
  107.    }
  108.   }
  109.   return null;
  110. }
  111. /**
  112.   * 获得多数据源方法
  113.   * @return DataSource
  114.   */
  115. public DataSource queryDataSourceByKey(String useKey) {
  116.   for (Map.Entry<String, DataSource> ds:this.dataSources.entrySet()) {
  117.    if (ds.getKey().equals(useKey)) {
  118.     return ds.getValue();
  119.    }
  120.   }
  121.   return null;
  122. }
  123. /**
  124.   * 数据源
  125.   */
  126. public DataSource getDataSource() {
  127.   return dataSource;
  128. }
  129. /**
  130.   * 数据源 setter 注入
  131.   */
  132. public void setDataSource(DataSource dataSource) {
  133.   this.dataSource = dataSource;
  134. }
  135. /**
  136.   * @return the isMultipleDataSource
  137.   */
  138. public boolean isMultipleDataSource() {
  139.   return isMultipleDataSource;
  140. }
  141. /**
  142.   * @param isMultipleDataSource the isMultipleDataSource to set
  143.   */
  144. public void setMultipleDataSource(boolean isMultipleDataSource) {
  145.   this.isMultipleDataSource = isMultipleDataSource;
  146. }
  147. /**
  148.   * @return the dataSources
  149.   */
  150. public Map<String, DataSource> getDataSources() {
  151.   return dataSources;
  152. }
  153. /**
  154.   * @param dataSources the dataSources to set
  155.   */
  156. public void setDataSources(Map<String, DataSource> dataSources) {
  157.   this.dataSources = dataSources;
  158. }
  159. /**
  160.   * 返回当前使用的数据源
  161.   * @return the dataSourceKey
  162.   */
  163. public String getDataSourceKey() {
  164.   return dataSourceKey;
  165. }
  166. /**
  167.   * 要使用的数据源为
  168.   * @param dataSourceKey the dataSourceKey to set
  169.   */
  170. public void setDataSourceKey(String dataSourceKey) {
  171.   this.dataSourceKey = dataSourceKey;
  172. }
  173. }
复制代码
回复

使用道具 举报

该用户从未签到

发表于 2011-8-2 21:43:10 | 显示全部楼层
好东东,顶一个。
回复 支持 反对

使用道具 举报

该用户从未签到

发表于 2011-8-2 21:43:24 | 显示全部楼层
学习了。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

GMT+8, 2024-5-27 13:06 , Processed in 0.341246 second(s), 34 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表