首页 > 代码库 > 查询数据的分页

查询数据的分页

在Mysql数据库中用的是   limit n,m    n代表着从第几条显示,m为显示的显示多少行

 数据类:成员变量,成员方法(get set)

public class Fruit {
	private String daihao;
	private String shuiguoming;
	private double danjia;
	private String chandi;
	private int kucun;
	private String tupian;
	public String getDaihao() {
		return daihao;
	}
	public void setDaihao(String daihao) {
		this.daihao = daihao;
	}
	public String getShuiguoming() {
		return shuiguoming;
	}
	public void setShuiguoming(String shuiguoming) {
		this.shuiguoming = shuiguoming;
	}
	public double getDanjia() {
		return danjia;
	}
	public void setDanjia(double danjia) {
		this.danjia = danjia;
	}
	public String getChandi() {
		return chandi;
	}
	public void setChandi(String chandi) {
		this.chandi = chandi;
	}
	public int getKucun() {
		return kucun;
	}
	public void setKucun(int kucun) {
		this.kucun = kucun;
	}
	public String getTupian() {
		return tupian;
	}
	public void setTupian(String tupian) {
		this.tupian = tupian;
	}
	
	
}

访问类:

  1.驱动类:

import java.sql.*;


public class DBConn {

    public static String qd="com.mysql.jdbc.Driver";
    public static String jdbc="jdbc:mysql://127.0.0.1:3306/mydb";
    public static String uid="root";
    public static String pwd="";
    
    public static Connection getconnection () throws Exception{
        Class.forName(qd);
        Connection conn=DriverManager.getConnection(jdbc,uid,pwd);
        
        return conn;
        
    }
}

    2.访问数据类

 

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

import com.maya.shuju.*;


public class FruitDao {
    private Connection conn;
    private ResultSet re;
    private PreparedStatement stat;
    
    public FruitDao() throws Exception {
    
        conn=DBConn.getconnection();
    }
    
    public ArrayList<Fruit> select(int m, int n  ) throws Exception{
        ArrayList<Fruit> list = new ArrayList<Fruit>();
        String sql="select * from fruit limit ?,?";
        stat=conn.prepareStatement(sql);
        stat.setInt(1, (m-1)*n);    //(m-1)*n,     m是跳过的行数,-1  再乘n   就是页数对应的行数
        stat.setInt(2, n);
        re=stat.executeQuery();
        while (re.next()){
            Fruit tada = new Fruit();
            
            tada.setDaihao(re.getString(1));
            tada.setShuiguoming(re.getString(2));
            tada.setDanjia(re.getDouble(3));
            tada.setChandi(re.getString(4));
            tada.setKucun(re.getInt(5));
            tada.setTupian(re.getString(6));
            list.add(tada);
        }

        return list;
    }

    
}

 

界面类:

import java.util.*;

import com.maya.fangwen.*;
import com.maya.shuju.*;

public class JieMian {

    public static void main(String[] args) throws Exception {
        
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入页数");
        int ys=Integer.parseInt(sc.nextLine());
        System.out.println("请输入每页几条");
        int ts=Integer.parseInt(sc.nextLine());
    
        
        ArrayList<Fruit> s=new FruitDao().select(ys, ts);
    
        for (int i=0;i<s.size();i++){
            System.out.println(s.get(i).getShuiguoming());
        }
        

    }

}

 

查询数据的分页