java MYSQL做分頁。本站提示廣大學習愛好者:(java MYSQL做分頁)文章只能為提供參考,不一定能成為您想要的結果。以下是java 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; } }