MySQL query statement for paging function
Statement:
SELECT * FROM table name where condition limit to find the first few pages, how many lines per page;
ImportJava.util.*;ImportJava.sql.*; Public classFruitdao {PrivateConnection Conn; PrivatePreparedStatement Pre; PrivateResultSet rs; PublicFruitdao ()throwsException {conn=Dbconnection.aa (); } //return Total pages Public intYeshu (intMeiyegeshu)throwssqlexception{//ask how many lines intYeshu = 0; String SQL= "SELECT count (*) from fruit"; Pre=conn.preparestatement (SQL); RS=Pre.executequery (); Rs.next (); intHangshu = Rs.getint (1); //ask how many pagesYeshu = (int) Math.ceil (1.0*hangshu/meiyegeshu);//take the upper valueConn.close (); returnYeshu; } //returns the data for the specified page PublicArraylist<fruit> Selest (intYaozhaoyeshu,intMeiyegeshu)throwsexception{ArrayList<Fruit> list =NewArraylist<fruit>(); String SQL= "SELECT * FROM fruit limit?,?"; Pre=conn.preparestatement (SQL); Pre.setint (1, meiyegeshu* (yaozhaoyeshu-1));//to find the first few pagesPre.setint (2, Meiyegeshu);//How many lines are there for the page you are looking forrs =Pre.executequery (); while(Rs.next ()) {Fruit data=NewFruit (); 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 (); returnlist; } }
Java MySQL do paging