java分頁查詢--oracle


在不改變報表查詢所有數據的sql的基礎上,調用一個方法來使sql變成分頁查詢:(在沒有order by 的基礎上)

public static void main(String[] args){
        StringBuffer sql = new StringBuffer();
        sql.append(" select uid,uname,usex,uage "); 
        sql.append(" from person where 1=1 ");
        
        String[] fields = { "id","name","sex","age"};    
      
        page( sql.toString(), fields);
      //  System.out.println( str.replaceFirst("select", "select rownum as rowno ,") );
    }
//分頁通用
    public String page(String sql,String[] fields){
        String json="";
        //根據查詢條件獲得的總數量
        String totalsql="select count(1) " +sql.substring(sql.indexOf("from"));
        int totalSize=getTotalSize(totalsql);
        System.out.println("分頁通用totalSize:"+totalSize);
        if(totalSize>0){
            //每頁大小
            int pageSize = this.request.getParameter("pageSize");//第幾頁
            String startPage=this.request.getParameter("startPage");
            //用來看第幾頁的數據
            int currentPage = 0;
            if (startPage != null) {
                currentPage = Integer.parseInt(startPage);
            }
            //一共多少頁
            int pageCount=totalSize%pageSize==0 ? totalSize/pageSize : totalSize/pageSize+1;
            //從第多少條數據開始,到多少條截止 
            int startCount=0;
            int endCount=0;
            if(currentPage !=0){
                startCount=currentPage*pageSize- pageSize + 1;
                if(currentPage*pageSize <= totalSize){
                    endCount=currentPage * pageSize;
                }
                if(currentPage*pageSize > totalSize){
                    endCount=totalSize % pageSize+(currentPage-1)*pageSize;
                }
            }
       //注意,將 select 替換后,sql 變成了 select rownum as rowno ,uid,uname,usex,uage from person where 1=1 sql
=sql.replaceFirst("select", "select rownum as rowno ,"); StringBuffer fysql=new StringBuffer(" select * from( ");  //注意 fysql 是: select * from( select rownum as rowno ,uid,uname,usex,uage from person where 1=1 and rownum<=10 ) table_alias where table_alias.rowno>=1
       fysql.append(sql); fysql.append(
" and rownum<= ") .append(endCount) .append(" ) table_alias") .append(" where table_alias.rowno>=") .append(startCount); System.out.println("分頁通用sql:"+fysql); List list = getYSList(fysql.toString()); JacksonUtil util=new JacksonUtil(); json=util.writeArrayJSON(fields, list); json="{pager:{pageCount:"+pageCount+",recordCount:"+totalSize+"},data:"+json+"}"; printResult("success", json); }else{ json = "{pager:{pageCount:0,recordCount:0},data:[]}"; printResult("success", json); } return null; }

根據查詢條件獲得的總數量

 public int  getTotalSize(String strSql) {
           
            Statement stmt = null;
            ResultSet rs = null;
            int totalSize=0;
            try
            {
        //數據庫的連接
Class.forName("oracle.jdbc.OracleDriver"); conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:oa", "123", "123"); stmt = conn.createStatement(); rs = stmt.executeQuery(strSql); if(rs.next()) { totalSize=Integer.parseInt(rs.getString(1)); } rs.close(); stmt.close(); conn.close(); } catch (Exception e) { e.printStackTrace(); } finally { if (rs != null) { rs = null; } if (stmt != null) { stmt = null; } if (conn != null){ conn = null; }return totalSize; }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM