九色国产,午夜在线视频,新黄色网址,九九色综合,天天做夜夜做久久做狠狠,天天躁夜夜躁狠狠躁2021a,久久不卡一区二区三区

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
mysql、sqlserver、oracle分頁,java分頁統(tǒng)一接口實現

定義:pageStart 起始頁,pageEnd 終止頁,pageSize頁面容量

oracle分頁:

    select * from ( select mytable.*,rownum num from (實際傳的SQL) where rownum<=pageEnd) where num>=pageStart

sqlServer分頁:

           select * from ( select top 頁面容量 from( select top  頁面容量*當前頁碼 * from 表 where 條件 order by 字段A) as temptable1 order by

字段A desc) as temptable2 order by 字段A  

Mysql分頁:

         select * from mytable where 條件 limit 當前頁碼*頁面容量-1 to 頁面容量

Java分頁接口和實現類:

 

package com.qg.demo.util;

import java.sql.Connection;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;

public class OracleUtil {
 private String dataSourceName;
 private DataSource ds;
 public OracleUtil(String dataSourceName){
  this.dataSourceName = dataSourceName;
 }
 public OracleUtil(){
  
 }
 public void setDataSourceName(String dataSourceName){
  this.dataSourceName = dataSourceName;
 }
 public void init(){
  Context initContext;
  try {
   initContext = new InitialContext();
   ds = (DataSource)initContext.lookup(dataSourceName);
  } catch (NamingException e) {
   e.printStackTrace();
  }
 }
 public int update(String sql,String[] param){
  int result = 0;
  QueryRunner qr = new QueryRunner(ds);
  try {
   result = qr.update(sql,param);
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return result;
 }
 public Object query(String sql,String[] param,ResultSetHandler rsh){
  QueryRunner qr = new QueryRunner(ds);
  Object result = null;
  try {
   result = qr.query(sql, param,rsh);
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return result;
 }
 public static Connection getConnection(){
  Connection conn = null;
  try {
   Context context = new InitialContext();
   DataSource ds = (DataSource)context.lookup("java:/comp/env/jdbc/oracleds");
   conn = ds.getConnection();
   QueryRunner qr = new QueryRunner(ds);
//   PreparedStatement pstmt = conn.prepareStatement("select * from guestbook");
//   ResultSet rs = pstmt.executeQuery();
//   while(rs.next()){
//       System.out.println(rs.getInt("g_id"));  
//    System.out.println(rs.getString("title"));
//    System.out.println(rs.getString("remark"));
//   }
   
  } catch (NamingException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }catch(SQLException e){
   e.printStackTrace();
  }
  return conn;
 }

}

 

        

package com.qg.demo.util;

import java.util.List;

public interface Pagination {
 public boolean isLast();
 public boolean isFirst();
 public boolean hasNext();
 public boolean hasPrevious();
 public int getMaxElements();//最大記錄數
 public int getMaxPage();//最大頁碼
 public int getNext();
 public int getPrevious();
 public int getPageSize();
 public int getPageNumber();
 public List<Object> getList();
 public void setPageSize(int pageSize);
 public void setPageNumber(int pageNumber);
}

 

 

package com.qg.demo.util;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.regex.Pattern;

import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

public class OraclePaginationImpl implements Pagination {
 private int pageSize = 20;
 private int pageNumber = 1;
 private int maxElements;
 private int maxPage;
 private String sql;
 private OracleUtil db;
 public  OraclePaginationImpl(String sql){
  this.sql = sql;
  init();
 }
 public OraclePaginationImpl(String sql,int pageSize, int pageNumber){
  this.sql = sql;
  this.pageSize = pageSize;
  this.pageNumber = pageNumber;
  init();
  setPageNumber(pageNumber);
 }
 private void init(){
  db = new OracleUtil("java:/comp/env/jdbc/oracleds");
  db.init();
  setMaxElements();
  setmaxPage();
 }
 private void setMaxElements() {
  //select * from xxx order by xx desc
  //select count(1) from xxx order by xx desc
  String regex = "select((.)+)from";
  Pattern p = Pattern.compile(regex,Pattern.CASE_INSENSITIVE);
  String[] s = p.split(this.sql);
  String newSql = "select count(1) as total from "+s[1];
  ResultSetHandler handler = new ResultSetHandler(){
   public Object handle(ResultSet rs) throws SQLException{
    if(rs.next()){
     return new Integer(rs.getInt("total"));
    }else{
     return null;
    }
   }
  };
  this.maxElements = (Integer)db.query(newSql, null, handler);
  
 }
 private void setmaxPage(){
  this.maxPage = (maxElements%pageSize == 0 ? maxElements/pageSize : (maxElements/pageSize +1));
  
 }
 private String sqlModify(String sql,int begin ,int end){
  StringBuffer buffer = new StringBuffer();
  buffer.append("select * from ( select rownum num,a.* from (")
        .append(sql)
        .append(") a where rownum <= ")
        .append(end)
        .append(") where num >= ")
        .append(begin);
  return buffer.toString();
 }
 private int getBeginElement() {
  return (pageNumber-1) * pageSize +1;
 }
 private int getEndElement() {
  return (pageNumber*pageSize >=maxElements ? maxElements : pageNumber*pageNumber);
 }
 public List<Object> getList() {
  String newSql = this.sqlModify(sql, getBeginElement(), getEndElement());
  return (List)db.query(sql, null, new MapListHandler());
 }

 public int getMaxElements() {
  return maxElements;
 }

 public int getMaxPage() {
  return maxPage;
 }

 public int getNext() {
   return pageNumber+1 >= maxPage ? maxPage : pageNumber+1;
 }

 public int getPageNumber() {
  return pageNumber;
 }

 public int getPageSize() {
  return pageSize;
 }

 public int getPrevious() {
  return pageNumber-1 <=1 ? 1 :pageNumber -1;
 }

 public boolean hasNext() {
  return pageNumber < maxPage; 
 }

 public boolean hasPrevious() {
  return pageNumber > 1;
 }

 public boolean isFirst() {
  return pageNumber == 1;
 }

 public boolean isLast() {
  return pageNumber == maxPage;
 }

 public void setPageNumber(int pageNumber) {
  if(pageNumber>maxPage){
   this.pageNumber = maxPage;
  }else if(pageNumber<1){
   this.pageNumber = 1;
  }else{
   this.pageNumber = pageNumber;
  }
 }

 public void setPageSize(int pageSize) {
  this.pageSize = pageSize;

 }
}

本站僅提供存儲服務,所有內容均由用戶發(fā)布,如發(fā)現有害或侵權內容,請點擊舉報
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
生成SQL語句分頁的通用C#函數
pg_Util分頁
JDBC 學習筆記(一)
常用公共代碼二之分頁代碼的實現
oracle中的分頁sql為什么這樣寫
做一個不復制粘貼的程序員[1]: 使用模板方法模式(1)- 分頁查詢實例
更多類似文章 >>
生活服務
熱點新聞
分享 收藏 導長圖 關注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服