1. 程式人生 > 程式設計 >java開發之Jdbc分頁原始碼詳解

java開發之Jdbc分頁原始碼詳解

總之是用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 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; 
 } 
 
}

更多關於java開發之Jdb分頁原始碼例項請檢視下面的相關連結