In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail how to use Java to achieve paging function. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
In a project, paging is essential in a project, which can prevent us from making a large number of data queries from the database slower and improve our query efficiency.
1. Define the paging model: PageModel
Package com.common.page; import java.util.List; / * encapsulates paging information * @ author Administrator * * / public class PageModel {/ / result set private List list; / / query records private int totalRecords; / / how many pieces of data per page private int pageSize; / / Page private int pageNo / * * Total number of pages * @ return * / public int getTotalPages () {return (totalRecords + pageSize-1) / pageSize;} / * get the home page * @ return * / public int getTopPageNo () {return 1;} / * previous page * @ return * / public int getPreviousPageNo () {if (pageNo = getBottomPageNo ()) {return getBottomPageNo () } return pageNo + 1;} / * get the last page * @ return * / public int getBottomPageNo () {return getTotalPages ();} public List getList () {return list;} public void setList (List list) {this.list = list;} public int getTotalRecords () {return totalRecords;} public void setTotalRecords (int totalRecords) {this.totalRecords = totalRecords;} public int getPageSize () {return pageSize } public void setPageSize (int pageSize) {this.pageSize = pageSize;} public int getPageNo () {return pageNo;} public void setPageNo (int pageNo) {this.pageNo = pageNo;}}
2. Paging test: create an admin table in MySQL with fields id, name and password
3. Entity bean class of resume Admin:
Package com.common.page; public class Admin {private int id; private String name; private String password; public int getId () {return id;} public void setId (int id) {this.id = id;} public String getName () {return name;} public void setName (String name) {this.name = name;} public String getPassword () {return password;} public void setPassword (String password) {this.password = password;}}
4. Test calls:
Package com.common.page; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import com.common.db.DbUtil; public class Client {public static PageModel findAdmins (int pageNo,int pageSize) {Connection conn=DbUtil.getConnection (); String sql= "select * from admin limit?,?"; PageModel pageModel=null; PreparedStatement pstm=null; ResultSet rs=null; Admin admin=null; List list=new ArrayList () Try {pstm=conn.prepareStatement (sql); pstm.setInt (1, (pageNo-1) * pageSize); pstm.setInt (2, pageNo*pageSize); rs=pstm.executeQuery (); while (rs.next ()) {admin=new Admin (); admin.setId (rs.getInt ("a_id")); admin.setName (rs.getString ("a_name")); admin.setPassword (rs.getString ("a_pwd")); list.add (admin) } ResultSet rs2=pstm.executeQuery ("select count (*) from admin"); int total=0; if (rs2.next ()) {total=rs2.getInt (1);} pageModel=new PageModel (); pageModel.setPageNo (pageNo); pageModel.setPageSize (pageSize); pageModel.setTotalRecords (total); pageModel.setList (list);} catch (SQLException e) {e.printStackTrace ();} finally {DbUtil.close (conn); DbUtil.close (pstm); DbUtil.close (rs) } return pageModel;} public static void main (String [] args) {PageModel pageModel=Client.findAdmins (2a.getId 4); List list=pageModel.getList (); for (Admin a:list) {System.out.print ("ID:" + a.getId () + ", username:" + a.getName () + ", password:" + a.getPassword ()); System.out.println ();} System.out.print ("current page:" + pageModel.getPageNo () + ") System.out.print ("Total" + pageModel.getTotalPages () + "Page"); System.out.print ("Home:" + pageModel.getTopPageNo () + "); System.out.print (" previous Page: "+ pageModel.getPreviousPageNo () +"); System.out.print ("next Page:" + pageModel.getNextPageNo () + "); System.out.print (" Last Page: "+ pageModel.getBottomPageNo () +") System.out.print ("Total" + pageModel.getTotalRecords () + "record"); System.out.println ();}}
This paging effect is achieved, we want to achieve the paging effect, as long as the input of the corresponding parameters and the corresponding database execution statement can be achieved, I hope you can use it flexibly.
This is the end of this article on "how to use Java to achieve paging function". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.