Java Web Simple pagination display Instance code _java

Source: Internet
Author: User
Tags java web

This article uses two methods: (1) to calculate the total number of pages. (2) Query the specified page data to achieve a simple paging effect.

train of thought: the first way to provide a paging query in the DAO object is to call the method in the control layer to find the data on the specified page, and display the page data through EL expressions and JSTL at the presentation layer.

First to show you the effect of the picture:

digression: This page display is to use the "presentation Layer-control layer-dao-database" design idea to achieve, what need to improve the place everyone put forward, common learning progress. No more nonsense, start into the subject, the detailed steps are as follows:

1.DAO Layer-Database

The Jdbcutils class is used to open and close the database, and the core code is as follows:

Import java.sql.Connection;
Import Java.sql.DriverManager;
Import java.sql.PreparedStatement;
Import Java.sql.ResultSet;
Import java.sql.SQLException; 
public class Jdbcutils {private Connection conn=null; private PreparedStatement pstmt=null;
/** * Connect Database * @return/public Connection Connect () {String user= "root";
String password= "1234";
String Driverclass = "Com.mysql.jdbc.Driver";
String Jdbcurl = "Jdbc:mysql://localhost:3306/book"; try {class.forname (driverclass); conn = Drivermanager.getconnection (jdbcurl, user, password);} catch (Exception e) {//
TODO auto-generated Catch block E.printstacktrace ();
Return conn; /** * Close Closes the database * @param conn * @param pstmt * @param resu/public void closed (Connection conn,preparedstatement pstm 
T,resultset result) {if (conn!= null) {try {conn.close ().} catch (SQLException e) {//TODO auto-generated catch block} } if (pstmt!= null) {try {pstmt.close ();} catch (SQLException e) {//TODO auto-generated catch block E.printstaCktrace (); } if (result!= null) {try {result.close ().} catch (SQLException e) {//TODO auto-generated catch block e.printstacktr
Ace (); }
}
}
}

The

Method GetPage () and Method Listuser () in the Userdao class are used to calculate the total number of pages and the data for the specified page of the query, with the core code as follows:

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.db.JDBCUtils;
The public class Userdao {/** * Calculates the total number of pages * @return/public int getpage () {int recordcount=0,t1=0,t2=0;
PreparedStatement Pstmt=null;
ResultSet Result=null;
Jdbcutils jdbc=new jdbcutils ();
Connection Conn=jdbc.connect ();
String sql= "SELECT COUNT (*) from books"; try {pstmt=conn.preparestatement (SQL); Result=pstmt.executequery (); Result.next (); Recordcount=result.getint (1); T1
=recordcount%5;
T2=RECORDCOUNT/5; catch (Exception e) {//TODO auto-generated catch block E.printstacktrace ();}
finally{Jdbc.close (conn, pstmt, result);} if (T1!= 0) {t2=t2+1;} return T2;
/** * Query the data for the specified page * @param pageno * @return/public list<user> listuser (int pageno) {PreparedStatement pstmt=null;
ResultSet Result=null;
List<user> list=new arraylist<user> ();
int pagesize=5;
int page= (pageNo-1) *5;Jdbcutils jdbc=new jdbcutils ();
Connection Conn=jdbc.connect ();
String sql= "SELECT * from books ' ORDER by ID limit?,?"; try {pstmt=conn.preparestatement (SQL); Pstmt.setint (1, page); Pstmt.setint (2, pageSize); Result=pstmt.executequery ()
;
while (Result.next ()) {User user=new User (); User.setid (Result.getint (1)); User.setname (Result.getstring (2));
User.setnumber (Result.getstring (3));
List.add (user); The catch (Exception e) {//TODO auto-generated catch block E.printstacktrace ();}
finally{Jdbc.close (conn, pstmt, result); }
}

The User class is used to store the queried data, and the core code is as follows:

public class User {
private int id;
private String name;
private String number;
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 GetNumber () {return number
;
}
public void Setnumber (String number) {
this.number = number;
}
}

2. Control layer

The Listuser class calls the Userdao object to query the data and assigns the page to display the data, the core code is as follows:

Import java.io.IOException;
Import Java.io.PrintWriter;
Import java.util.ArrayList;
Import java.util.List;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
Import Com.dao.User;
Import Com.dao.UserDao; public class Listuser extends HttpServlet {public listuser () {super (),} public void Destroy () {Super.destroy ();//Jus T puts "destroy" string in Log//Put your code here} public void doget (HttpServletRequest request, HttpServletResponse R Esponse) throws Servletexception, IOException {doPost (request, response);} public void DoPost (HttpServletRequest reques T, HttpServletResponse response) throws Servletexception, IOException {response.setcharacterencoding ("utf-8"); int
PageNo = 1;
Userdao userdao=new Userdao ();
List<user> lists=new arraylist<user> ();
String pageno=request.getparameter ("Pagenos");
if (PageNo!= null) {pageno=integer.parseint (PageNo);} lists=userdao.listuser (PageNo);
int Recordcount=userdao.getpage ();
Request.setattribute ("RecordCount", Userdao.getpage ());
Request.setattribute ("Listss", lists);
Request.setattribute ("Pagenos", PageNo);
Request.getrequestdispatcher ("userlist.jsp"). Forward (request, response); public void Init () throws Servletexception {//Put your code here}}

3. Presentation Layer

Output page userlist.jsp, using EL and JSTL output query results, the core code is as follows:

<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%> <%@ taglib prefix= "C" uri= "http://" Java.sun.com/jsp/jstl/core "%> <%@ taglib uri=" http://java.sun.com/jsp/jstl/fmt "prefix=" FMT "%> <%@
Taglib uri= "http://java.sun.com/jsp/jstl/functions" prefix= "FN"%> <% String Path = Request.getcontextpath ();
String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/"; %> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >  

The above is a small series to introduce the Java Web simple pagination display instance code, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.