0% found this document useful (0 votes)
39 views3 pages

Pagination

The document outlines a Java implementation for pagination by fetching the total number of records from a database and calculating total pages. It includes code snippets for a Pagination class, methods to retrieve records, and a JSP page for displaying the paginated data. The approach ensures efficient data handling by directly querying the database for the total records and paginated results.

Uploaded by

toyow26421
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views3 pages

Pagination

The document outlines a Java implementation for pagination by fetching the total number of records from a database and calculating total pages. It includes code snippets for a Pagination class, methods to retrieve records, and a JSP page for displaying the paginated data. The approach ensures efficient data handling by directly querying the database for the total records and paginated results.

Uploaded by

toyow26421
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Got it!

Let's adjust our approach to calculate the total number of pages and total
elements directly from the database query. Here's how you can do it:

1. **Fetch the total number of records and calculate total pages from the
database:**

```java
public class Pagination {
private int totalRecords;
private int recordsPerPage;
private int currentPage;

public Pagination(int totalRecords, int recordsPerPage, int currentPage) {


this.totalRecords = totalRecords;
this.recordsPerPage = recordsPerPage;
this.currentPage = currentPage;
}

public int getTotalPages() {


return (totalRecords + recordsPerPage - 1) / recordsPerPage;
}
}
```

2. **Get the total number of records from the database:**

```java
public int getTotalRecords() {
String query = "SELECT COUNT(*) FROM your_table";
int totalRecords = 0;

try (Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);


PreparedStatement ps = conn.prepareStatement(query);
ResultSet rs = ps.executeQuery()) {
if (rs.next()) {
totalRecords = rs.getInt(1);
}
} catch (SQLException e) {
e.printStackTrace();
}

return totalRecords;
}
```

3. **Fetch paginated data from the database:**

```java
public List<Record> fetchRecords(int offset, int recordsPerPage) {
List<Record> records = new ArrayList<>();
String query = "SELECT * FROM your_table LIMIT ?, ?";

try (Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);


PreparedStatement ps = conn.prepareStatement(query)) {
ps.setInt(1, offset);
ps.setInt(2, recordsPerPage);
ResultSet rs = ps.executeQuery();

while (rs.next()) {
Record record = new Record();
// Populate the record object with data from the result set
records.add(record);
}
} catch (SQLException e) {
e.printStackTrace();
}

return records;
}
```

4. **Controller Servlet:**

```java
int recordsPerPage = 10;
int currentPage = Integer.parseInt(request.getParameter("currentPage"));
int totalRecords = getTotalRecords();

Pagination pagination = new Pagination(totalRecords, recordsPerPage,


currentPage);
int totalPages = pagination.getTotalPages();
int offset = (currentPage - 1) * recordsPerPage;

List<Record> records = fetchRecords(offset, recordsPerPage);

request.setAttribute("records", records);
request.setAttribute("totalPages", totalPages);
request.setAttribute("currentPage", currentPage);
request.getRequestDispatcher("your_jsp_page.jsp").forward(request, response);
```

5. **JSP Page:**

```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<html>
<head>
<title>Pagination Example</title>
</head>
<body>
<table>
<thead>
<tr>
<th>Column 1</th>
<th>Column 2</th>
<!-- Add more columns as needed -->
</tr>
</thead>
<tbody>
<c:forEach var="record" items="${records}">
<tr>
<td>${record.column1}</td>
<td>${record.column2}</td>
<!-- Add more columns as needed -->
</tr>
</c:forEach>
</tbody>
</table>

<nav aria-label="Page navigation">


<ul class="pagination">
<c:forEach var="i" begin="1" end="${totalPages}">
<li class="${i == currentPage ? 'active' : ''}"><a
href="your_servlet?currentPage=${i}">${i}</a></li>
</c:forEach>
</ul>
</nav>
</body>
</html>
```

This should help you achieve pagination by fetching the total number of records and
calculating the total pages directly from the database query. If you need further
assistance or have any questions, feel free to ask!

You might also like