首先定义一个fruit表,表里含有很多数据:
定义一个数据文件:
public class Fruit { public String getIds() { return ids; } public void setIds(String ids) { this.ids = ids; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public String getSource() { return source; } public void setSource(String source) { this.source = source; } public int getNumbers() { return numbers; } public void setNumbers(int numbers) { this.numbers = numbers; } public String getImage() { return image; } public void setImage(String image) { this.image = image; } private String ids; private String name; private double price; private String source; private int numbers; private String image; }
定义一个数据连接访问:
import java.sql.*; import java.util.*; public class DBConnection { private static String drv="com.mysql.jdbc.Driver"; private static String url="jdbc:mysql://127.0.0.1:3306/mydb?characterEncoding=GBK"; private static String uid="root"; private static String pwd=""; public static Connection getConnection() throws Exception{ Class.forName(drv); Connection conn = DriverManager.getConnection(url,uid,pwd); return conn; } }
import java.util.*; import java.sql.*; public class FruitDao { private Connection conn; private PreparedStatement stat; private ResultSet rs; public FruitDao() throws Exception{ conn = DBConnection.getConnection(); } //返回总页数 public int getPageCount(int pageSize) throws Exception{ int pageCount = 0; //求总行数 rowCount String sql = "select count(*) from fruit"; stat = conn.prepareStatement(sql); rs = stat.executeQuery(); rs.next(); int rowCount = rs.getInt(1); //求总页数 pageCount = (rowCount/pageSize)--进位 pageCount = (int)Math.ceil( 1.0*rowCount/pageSize); conn.close(); return pageCount; } //返回指定页的数据 public ArrayList<Fruit> select(int pageSize,int pageNo) throws Exception{ ArrayList<Fruit> list = new ArrayList<Fruit>(); String sql = "select * from fruit limit ?,?"; //select TOP 2 * from Fruit where Ids NOT IN (SELECT TOP 4 Ids from fruit) 这是sqlserver的分页语句 stat = conn.prepareStatement(sql); stat.setInt(1, (pageNo-1)*pageSize); stat.setInt(2, pageSize); rs = stat.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; } //增删改查的方法....略 }
然后是界面文件:
import java.awt.print.Pageable; import java.util.*; public class Test { private static final int PAGESIZE=2; public static void main(String[] args) throws Exception { int pageCount = new FruitDao().getPageCount(PAGESIZE); System.out.println("一共"+pageCount+"页,每页"+PAGESIZE+"个"); ArrayList<Fruit> list = new FruitDao().select(PAGESIZE, 1); for(int i=0;i<list.size();i++){ System.out.println(list.get(i).getIds()+" "+list.get(i).getName()+list.get(i).getPrice()+list.get(i).getSource()); } } }
输出结果为:
一共4页,每页2个 k001 苹果 2.4 烟台 k002 菠萝 1.4 广东
时间: 2024-10-07 15:22:42