Java pagination Content Instance detailed

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.