程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA綜合教程 >> java MYSQL做分頁,javamysql分頁

java MYSQL做分頁,javamysql分頁

編輯:JAVA綜合教程

java MYSQL做分頁,javamysql分頁


MySql中查詢語句實現分頁功能

語句:

select * from 表名 where 條件 limit 要找第幾頁,每頁多少行;

import java.util.*;
import java.sql.*;

public class FruitDao {
    private Connection conn;
    private PreparedStatement pre;
    private ResultSet rs;
    public FruitDao() throws Exception {
        conn = DBConnection.aa();
    }
    //返回總頁數
    public int yeshu(int meiyegeshu) throws SQLException{
        //求有多少行
        int yeshu = 0;
        String sql = "select count(*)from fruit";
        pre = conn.prepareStatement(sql);
        rs = pre.executeQuery();
        rs.next();
        int hangshu = rs.getInt(1);
        //求有多少頁
        yeshu = (int)Math.ceil(1.0*hangshu/meiyegeshu);//取上限值
        conn.close();
        return yeshu;    
    }
    
    
    //返回指定頁的數據
    public ArrayList<Fruit> selest(int yaozhaoyeshu,int meiyegeshu) throws Exception{
        ArrayList<Fruit> list = new ArrayList<Fruit>();
        String sql = "select * from fruit limit ?,?";
        pre = conn.prepareStatement(sql);
        pre.setInt(1, meiyegeshu*(yaozhaoyeshu-1));//要找第幾頁,從多少行開始 
        pre.setInt(2, meiyegeshu);//找的頁有多少行
        rs = pre.executeQuery();
        while (rs.next()){
            Fruit data = new Fruit();
            data.setIds(rs.getString(1));
            data.setName(rs.getString(2));
            data.setPrice(rs.getDouble(3));
            data.setSource(rs.getString(4));
            data.setNumbers(rs.getInt(5));
            data.setImage(rs.getString(6));
            list.add(data);
        }
        conn.close();    
        return list;
    }
    

}
 

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved