java开发之Jdbc分页源码详解
作者:mianhuaman 时间:2021-10-28 16:06:48
总之是用jdbc 的游标移动
package com.sp.person.sql.util;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.TreeMap;
import javax.sql.DataSource;
/**
* 常常有同事在问JDBC 分页查询 这里给二个例子介绍一下
* JDBC 分页查询
* 分成二种方式希望对大家有所帮助
* 分另表示了absolute 与relative 的区别
* @see 这时用到一个数据源设计模式
* 数据源与数据库连接没有关系
* 例用接口回调的特性
* @author liuqing
* @version 1.0
*
*/
public class JdbcUtil {
/**
* 数据源
*/
private DataSource dataSource;
/**
* 不否启用多数据源
*/
private boolean isMultipleDataSource;
/**
* 有时一个系统可能要使用多个数据源,存放多个数据源
*/
private Map<String,DataSource> dataSources = new TreeMap<String,DataSource>();
/**
* if true isMultipleDataSource is Key to DataSource
*/
private String dataSourceKey;
/**
* 默认构造器
*/
public JdbcUtil() {
}
/**
* 构造器 Spring 的构造器注入
* @param dataSource
*/
public JdbcUtil(DataSource dataSource) {
this.dataSource = dataSource;
}
/**
* JDBC 分页查询
* @param sql SQL 查询语句
* @param firstSize 起始页
* @param maxSize 返回数据条数
* @return ResultSet
* @throws SQLException
*/
public ResultSet queryPageAbsolute(String sql,
int firstSize,int maxSize) throws SQLException {
PreparedStatement pre = this.getConn().prepareStatement(sql);
pre.setMaxRows(maxSize);
ResultSet rs = pre.executeQuery();
rs.absolute(firstSize * maxSize);
return rs;
}
/**
* JDBC 分页查询
* @param sql SQL 查询语句
* @param firstSize 起始页
* @param maxSize 返回数据条数
* @return ResultSet 返回结果集
* @throws SQLException
*/
public ResultSet queryPageRelative(String sql,
int firstSize,int maxSize) throws SQLException {
PreparedStatement pre = getConn().prepareStatement(sql);
pre.setMaxRows(maxSize);
ResultSet rs = pre.executeQuery();
rs.relative(firstSize);
return rs;
}
/**
*
* @return Connection
* @throws SQLException
*/
private Connection getConn() throws SQLException {
//使用多数据源的情况
if (this.isMultipleDataSource) {
DataSource v_dataSource = this.queryDataSourceByKey();
if (v_dataSource != null) {
return v_dataSource.getConnection();
}
}
return this.dataSource.getConnection();
}
/**
* 获得多数据源方法
* @return DataSource
*/
public DataSource queryDataSourceByKey() {
for (Map.Entry<String, DataSource> ds:this.dataSources.entrySet()) {
if (ds.getKey().equals(dataSourceKey)) {
return ds.getValue();
}
}
return null;
}
/**
* 获得多数据源方法
* @return DataSource
*/
public DataSource queryDataSourceByKey(String useKey) {
for (Map.Entry<String, DataSource> ds:this.dataSources.entrySet()) {
if (ds.getKey().equals(useKey)) {
return ds.getValue();
}
}
return null;
}
/**
* 数据源
*/
public DataSource getDataSource() {
return dataSource;
}
/**
* 数据源 setter 注入
*/
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
/**
* @return the isMultipleDataSource
*/
public boolean isMultipleDataSource() {
return isMultipleDataSource;
}
/**
* @param isMultipleDataSource the isMultipleDataSource to set
*/
public void setMultipleDataSource(boolean isMultipleDataSource) {
this.isMultipleDataSource = isMultipleDataSource;
}
/**
* @return the dataSources
*/
public Map<String, DataSource> getDataSources() {
return dataSources;
}
/**
* @param dataSources the dataSources to set
*/
public void setDataSources(Map<String, DataSource> dataSources) {
this.dataSources = dataSources;
}
/**
* 返回当前使用的数据源
* @return the dataSourceKey
*/
public String getDataSourceKey() {
return dataSourceKey;
}
/**
* 要使用的数据源为
* @param dataSourceKey the dataSourceKey to set
*/
public void setDataSourceKey(String dataSourceKey) {
this.dataSourceKey = dataSourceKey;
}
}
来源:https://www.iteye.com/blog/mianhuaman-1137995
标签:java,Jdbc,分页
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
mybatis的插件机制示例详解
2023-02-24 23:46:17
tomcat部署java web项目遇到的问题及解决方法
2023-08-04 23:44:27
![](https://img.aspxhome.com/file/2023/2/58032_0s.jpg)
SSH框架网上商城项目第10战之搭建商品类基本模块
2023-11-12 14:00:29
![](https://img.aspxhome.com/file/2023/6/59006_0s.png)
Java面试synchronized偏向锁后hashcode存址
2023-08-09 09:15:06
![](https://img.aspxhome.com/file/2023/0/57920_0s.png)
一文精通Java中的volatile关键字
2023-11-24 04:17:14
![](https://img.aspxhome.com/file/2023/2/59572_0s.png)
Spring实现内置监听器
2021-07-06 01:29:06
![](https://img.aspxhome.com/file/2023/8/63318_0s.png)
springboot+mybatis+redis 二级缓存问题实例详解
2022-08-09 09:06:36
![](https://img.aspxhome.com/file/2023/8/63148_0s.jpg)
详解JAVA动态代理
2023-11-24 22:52:04
自定义mybatis插件如何实现sql日志打印
2023-11-29 11:22:37
![](https://img.aspxhome.com/file/2023/0/60670_0s.png)
Struts2下拉框实例解析
2021-09-16 16:29:59
![](https://img.aspxhome.com/file/2023/9/61399_0s.jpg)
spring springMVC中常用注解解析
2023-09-14 20:45:46
java中关于深拷贝的几种方式总结
2023-12-13 17:39:41
![](https://img.aspxhome.com/file/2023/4/62054_0s.png)
Spring Boot调用 Shell 脚本实现看门狗功能
2023-09-14 18:52:07
java 引用传递的三种类型小结
2023-09-03 03:59:47
![](https://img.aspxhome.com/file/2023/0/60560_0s.png)
聊聊Java 中的线程中断
2021-05-31 02:04:30
浅析SpringBoot2底层注解@Conditional@ImportResource
2023-08-01 23:35:51
![](https://img.aspxhome.com/file/2023/3/61793_0s.png)
Java关键字instanceof用法及实现策略
2023-09-23 06:05:38
![](https://img.aspxhome.com/file/2023/0/58560_0s.png)
Java 中 synchronized的用法详解(四种用法)
2022-03-11 08:55:05
Java详细讲解不同版本的接口语法和抽象类与接口的区别
2022-09-30 01:46:38
![](https://img.aspxhome.com/file/2023/5/61875_0s.png)
JAVA WSIMPORT生成WEBSERVICE客户端401认证过程图解
2023-11-14 00:27:55
![](https://img.aspxhome.com/file/2023/5/58985_0s.png)