First define a fruit table with lots of data:
Define a data file:
Public classFruit { PublicString getids () {returnIDs; } Public voidSetids (String IDs) { This. ids =IDs; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } Public DoubleGetPrice () {returnPrice ; } Public voidSetprice (DoublePrice ) { This. Price =Price ; } PublicString GetSource () {returnsource; } Public voidSetSource (String source) { This. Source =source; } Public intgetnumbers () {returnnumbers; } Public voidSetnumbers (intnumbers) { This. Numbers =numbers; } PublicString getImage () {returnimage; } Public voidsetimage (String image) { This. Image =image; } PrivateString IDs; PrivateString name; Private DoublePrice ; PrivateString Source; Private intnumbers; PrivateString image;
Define a data connection access:
ImportJava.sql.*;ImportJava.util.*; Public classDBConnection {Private StaticString drv= "Com.mysql.jdbc.Driver"; Private StaticString url= "JDBC:MYSQL://127.0.0.1:3306/MYDB?CHARACTERENCODING=GBK"; Private StaticString uid= "Root"; Private StaticString pwd= ""; Public StaticConnection getconnection ()throwsexception{Class.forName (DRV); Connection Conn=drivermanager.getconnection (URL,UID,PWD); returnConn; }}
ImportJava.util.*;ImportJava.sql.*; Public classFruitdao {PrivateConnection Conn; PrivatePreparedStatement Stat; PrivateResultSet rs; PublicFruitdao ()throwsexception{Conn=dbconnection.getconnection (); } //return Total pages Public intGetpagecount (intPageSize)throwsexception{intPageCount = 0; //total number of rows RowCountString sql = "SELECT COUNT (*) from fruit"; Stat=conn.preparestatement (SQL); RS=Stat.executequery (); Rs.next (); intRowCount = Rs.getint (1); //Total Pages PageCount = (rowcount/pagesize)--RoundingPageCount = (int) Math.ceil (1.0*rowcount/pageSize); Conn.close (); returnPageCount; } //returns the data for the specified page PublicArraylist<fruit> Select (intPageSize,intPageNo)throwsexception{ArrayList<Fruit> list =NewArraylist<fruit>(); String SQL= "SELECT * FROM fruit limit?,?";//Select top 2 * from Fruit where Ids not in (select Top 4 Ids from Fruit) This is a paging statement for SQL ServerStat =conn.preparestatement (SQL); Stat.setint (1, (pageNo-1) *pageSize); Stat.setint (2, pageSize); RS=Stat.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; } //How to change the method of adding and deleting .... Slightly}
Then the interface file:
Importjava.awt.print.Pageable;ImportJava.util.*; Public classTest {Private Static Final intpagesize=2; Public Static voidMain (string[] args)throwsException {intPageCount =NewFruitdao (). Getpagecount (PAGESIZE); System.out.println ("Total" +pagecount+ "page," +pagesize+ "per page"); ArrayList<Fruit> list =NewFruitdao (). Select (PAGESIZE, 1); for(intI=0;i<list.size (); i++) {System.out.println (List.get (i). GetIDs ()+ "" +list.get (i). GetName () +list.get (i). GetPrice () +List.get (i). GetSource ()); } }}
The output is:
2.4 Yantai k002 Pineapple 1.4
Java pagination Content Instance detailed