Paging Records in JSP

posted by Scudyen date: Thursday, May 22, 2008 category: JSP

 

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 + "<br>");
int cPage;
cPage = (pages / 10) + 1;
if ((cPage * 10) + 1 >= pages)
{
cPage++;
}

out.println("Count of Pages : " + (cPage - 1) + "<br><p><p>");
int p;
for(p = 1; p < cPage ; p++)
{
out.println("<a href=PAGE_NAME.jsp?mv=" + p + ">"+ p +"</a>&nbsp;|&nbsp;");
}

out.println("<hr>");

// 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 + "<br>");
}


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 ....

}

%>


Read More

Custom Search



Gazatem Technologies Active News Manager


page counter

Listed with 411asp.net Listed with 4aspin.com