Heim > Java > javaLernprogramm > Paginierung in Java

Paginierung in Java

WBOY
Freigeben: 2024-08-30 15:08:19
Original
1110 Leute haben es durchsucht

Java Pagination concept is applied for moving among the pages with First Page, Second Page, Third Page, Fourth Page etc. Buttons or Links. Pagination main motto is to move among the content immediately by clicking links or buttons. Java Pagination has multiple links or buttons provided for First Page, Second Page, Third Page, Fourth Page, etc. Create First Page, Second Page, Third Page, Fourth Page etc., buttons in Java; we have Servlets to achieve this.

What is a Bootstrap Pager?

Java Pagination concept is used to access the content by using First Page, Second Page, Third Page, Fourth Page, etc., buttons or more links or buttons based on client requirement to smoothly access the content.

ADVERTISEMENT Popular Course in this category JAVA MASTERY - Specialization | 78 Course Series | 15 Mock Tests

Why we Use JavaScript Pagination?

Given below shows why we use JavaScript Pagination:

Real-Time Scenario:

Let’s take the amazon website or Flipkart website for displaying available products in their database. Let suppose they have 1 million products with them. If they are trying to show all the items at a time, the customer must wait more time, like one day, to see all the product lists.

How Should we Tackle this Situation?

  • Instead of showing all the item at a time, we can show them 50 to 100 items at a time by using a list of link buttons.
  • If the customer is not satisfied with the first 50 to 100 products, then he will move to the next 50 to 100 items so on. This concept is called Pagination.

Creating Pagination Project Step by Step

  1. Create any class with setter and getters for adding values to the list.
  2. Create Servlet class for Pagination Logic.
  3. Create a class to add list values to any database to see these values on the output view page.
Note: Here MySQL database used; make sure you must install the MySQL server on your PC. Use mysql-connector-java.jar file to work MySQL database code.

      4. Create an HTML view page for viewing pagination.

Syntax:

Servlet Syntax:

//create a setter and getter class
public class Customer {
private int id;
private String name;
private float salary;
//setters and getters
}
//for pagination logic in servlet class
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter printWriterOut=response.getWriter();
String stringPageNumber=request.getParameter("page");
int paginationPageID=Integer.parseInt(stringPageNumber);
int toalCount=pageNumbers;
if(paginationPageID==1){}
else{
paginationPageID=paginationPageID-1;
paginationPageID=paginationPageID*toalCount+1;
}
}
//database connection for getting customer values
public static Connection getConnection(){
Connection con=null;
try{
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");
}catch(Exception e){System.out.println(e);}
return con;
}
//view output html page
<body>
<div class="a">
<a href="PaginationServlet?page=1">View Customer Details</a>
</div>
</body>
Nach dem Login kopieren

Examples of Pagination in Java

Above mentioned each step we have taken as a single example for better understanding. Once you have done all the examples, your project structure must be like the below in Eclipse; otherwise, it may not work.

Create a Dynamic web project and add all the below examples as like below:

Paginierung in Java

Note: Use Apache Tomcat server 7.0.

Example #1

Creating customer class.

Java Code: Customer.java

package com.pagination.setget;
public class Customer {
private int id;
private String name;
private float salary;
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 float getSalary() {
return salary;
}
public void setSalary(float salary) {
this.salary = salary;
}
}
Nach dem Login kopieren

Example #2

Creating servlet class for pagination logic.

Java Servlet Code: Pagination.java

package com.pagination.view;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.pagination.main.Pagination;
import com.pagination.setget.*;
@SuppressWarnings("serial")
@WebServlet("/PaginationServlet")
public class ViewPagination extends HttpServlet {
protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
httpServletResponse.setContentType("text/html");
PrintWriter printWriterOut=httpServletResponse.getWriter();
String stringPageNumber=httpServletRequest.getParameter("page");
int paginationPageID=Integer.parseInt(stringPageNumber);
int toalCount=5;
if(paginationPageID==1){}
else{
paginationPageID=paginationPageID-1;
paginationPageID=paginationPageID*toalCount+1;
}
List<Customer> customerList=Pagination.getRecords(paginationPageID,toalCount);
printWriterOut.print("<h2 style='color:green;text-align:center'>Introduction to Servlet Pagination</h2>");
printWriterOut.print("<h3 style='color:blue;text-align:center'>Customer Details in Table Format</h3>");
printWriterOut.print("<h1 style='color:brown'>We are in Page number=>"+stringPageNumber+"</h1>");
printWriterOut.print("<table style='color:navy' border='2' cellpadding='4' width='80%'>");
printWriterOut.print("<tr><th>Customer ID</th><th>Customer Name</th><th>Customer Salary</th>");
for(Customer customer:customerList){
printWriterOut.print("<tr><td>"+customer.getId()+"</td><td>"+customer.getName()+"</td><td>"+customer.getSalary()+"</td></tr>");
}
printWriterOut.print("</table>");
printWriterOut.print("<a href='PaginationServlet?page=1'>First Page||</a> ");
printWriterOut.print("<a href='PaginationServlet?page=2'>Second Page||</a> ");
printWriterOut.print("<a href='PaginationServlet?page=3'>Third Page||</a> ");
printWriterOut.print("<a href='PaginationServlet?page=4'>Fourth Page||</a> ");
printWriterOut.print("<a href='PaginationServlet?page=5'>Fifth Page</a> ");
printWriterOut.close();
}
}
Nach dem Login kopieren

Example #3

Create MySQL database code for saving list values.

Java Code: MySQLPagination.java

package com.pagination.main;
import com.pagination.setget.*;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class Pagination {
public static Connection getConnection(){
Connection connection=null;
try{
Class.forName("com.mysql.jdbc.Driver");
connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");
}catch(Exception e){System.out.println(e);}
return connection;
}
public static List<Customer> getRecords(int start,int total){
List<Customer> list=new ArrayList<Customer>();
try{
Connection connection=getConnection();
PreparedStatement preparedStatement=connection.prepareStatement("select * from customer limit "+(start-1)+","+total);
ResultSet rs=preparedStatement.executeQuery();
while(rs.next()){
Customer customer=new Customer();
customer.setId(rs.getInt(1));
customer.setName(rs.getString(2));
customer.setSalary(rs.getFloat(3));
list.add(customer);
}
connection.close();
}catch(Exception e){System.out.println(e);}
return list;
}
}
Nach dem Login kopieren

Example #4

View HTML page.

HTML Code: ViewPagination.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Pagination</title>
<style type="text/css">
.a {
text-align: center;
}
</style>
</head>
<body>
<div class="a">
<a href="PaginationServlet?page=1">View Customer Details</a>
</div>
</body>
</html>
Nach dem Login kopieren

Output:

Paginierung in Java

Paginierung in Java

Explanation:

  • In the first example, we have created a Customer setter and getter class.
  • In the second example, we have created the Pagination servlet class for adding pagination logic.
  • In the third example, we have created a MySQL database for adding list values for showing in the pagination view.
  • In the fourth example, we have created a view page by using an HTML page.

Conclusion – Pagination in Java

Pagination of Java is used to move between the pages by using buttons or links immediately. Pagination in Java can be done with Servlet and HTML y using MySQL jar file.

Das obige ist der detaillierte Inhalt vonPaginierung in Java. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Verwandte Etiketten:
Quelle:php
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage