java MYSQL做分页

Posted 滥好人

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java MYSQL做分页相关的知识,希望对你有一定的参考价值。

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;
    }
    

}
 

 

以上是关于java MYSQL做分页的主要内容,如果未能解决你的问题,请参考以下文章

MySQL分表后,如何做分页查询?

MySQL分表后,如何做分页查询?

mysql如何做分页查询?

在php里面用ajax怎么做分页

MySQL 百万级数据,怎么做分页查询?

Mysql千万级别数据如何 做分页?