Popular Articles | Latest Articles | Categories | RSS Feeds

Paging Records in JSP

Published on 2/16/2008 by Site Editor
More articles in JSP



 Vote this tutorial:

 

You can divide database results into pages with this script. This source code is for available to use with MySQL. But you can modify the code to use with PostgreSql and  Oracle. You can not use this code with MS-SQL. Becuase MS-SQL does not support using 'LIMIT' in your sql queries.

 

<%@ page import="java.sql.*" %>

<%

int pages;
Connection ocon = null;
Class.forName("org.gjt.mm.mysql.Driver");
ocon = DriverManager.getConnection("jdbc:mysql:///YOUR_ATABASE", "USERNAME", "PASSWORD");
Statement stmtt = ocon.createStatement();
ResultSet rc = stmtt.executeQuery("Select count(*) from YOUR_TABLE ");
rc.next();
pages = rc.getInt(1);
out.println("Count of Records : " + pages + "
");
int cPage;
cPage = (pages / 10) + 1;
if ((cPage * 10) + 1 >= pages)
{
cPage++;
}

out.println("Count of Pages : " + (cPage - 1) + "

 

");
int p;
for(p = 1; p < cPage ; p++)
{
out.println("PAGE_NAME.jsp?mv=" + p + ">"+ p +" | ");
}

out.println("


");

// paging

int cpage;
int currentRs;
String pt;

pt = request.getParameter("mv");

if (pt == null)
{
currentRs = 0;
}
else
{
cpage = Integer.parseInt((String)pt);
currentRs = 10 * (cpage - 1);
out.println(cpage + "
");
}


Connection con = null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection("jdbc:mysql:///YOUR_ATABASE", "USERNAME", "PASSWORD");
Statement stmt = con.createStatement();
String sql;
sql = "Select * from YOUR_TABLE LIMIT "+ currentRs +",10";
ResultSet rs = stmt.executeQuery(sql);
//rs.absolute(10);

while (rs.next())
{
out.println();

 

// add your table fields here ....

}

%>


Comments:
no comments submitted


Only members can write comments.Please, login / register to write comment.

Latest Posts

Active News Manager - news management system




Welcome to Dotnetindex.com:Login  |  Register
Free software:
Giga RSS Reader
IkonAdman
QMailing list
Active News
Download Manager (PRO)
Download Manager (Lite)

Search Dotnetindex.com:


Who's online

  • There 36 online users.

Latest tutorialsBuilding ASP.NET Web Forms to Use a MySQL Database
A Guide to Upgrading Your XP Machine for Optimum Performance with Windows 7
Retrieving Data from Microsoft SQL Server 2008 Using ASP.NET 3.5
Creating an ASP.NET Dynamic Web Page Using MS SQL Server 2008 Database (GridView Display)
Inserting Data into a Microsoft SQL 2008 Database in ASP.NET 3.5
Using C#, ASP.NET, and ADO.NET To Develop A Multi-Tier App From Start To Finish
URL Routing in ASP.NET 4.0
Integrating Twitter Into An ASP.NET Website
Creating Multiple Choice Exam Application Using ASP.NET MVC Framework
Performance: Using dynamic code to copy property values of two objects
A Look at the GridView's New Sorting Styles in ASP.NET 4.0
.NET LINQ from Scratch
The Kayak Framework: An easy way to speak HTTP with .NET
How to Create a Custom Transform in UrlRewriter.net
Applet and Servlet Communication
ASP.NET Resources, Reviews

ASP Resources, Reviews

PHP, ASP, .NET, JSP Resources, Reviews

Hotscripts.com Free Photoshop, Flash and 3DS MAX tutorials