.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
david stephan
Gaurav Pal

Home >> Articles >> General >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Paging in Java Server Page

Posted By:satyapriyanayak       Posted Date: October 09, 2014    Points: 200    Category: General    URL: http://www.dotnetspark.com  

In this article we will know how to do a paging in jsp. In this paging, per page 10 records will be shown, if it exceeds then other records will be shown in the second link when user clicks that link.

Here we use Type-1 Driver (JDBC:ODBC bridge driver) for connection to Oracle database

Creation of dsn(database source name) for Oracle

Start-Control panel- Administrative Tools- Data Sources (ODBC)-go to system dsn tab-click add button-select a driver for which you want to set up data source (for Oracle- Oracle in XE)-select it and click finish-give any name in data source name textbox-then click ok button.

Note: - Here Username=system, Password=pintu and Dsn name=dsn1

Table Creation with data

Create table emp(rownumber int,empno varchar(50),ename varchar(50),eaddress varchar(50),sal int)

insert into emp values(1,'e001','Raj','Delhi',1000)
insert into emp values(2,'e002','Raju','Mumbai',2000)
insert into emp values(3,'e003','Rajesh','Pune',3000)
insert into emp values(4,'e004','Ravi','Noida',4000)
insert into emp values(5,'e005','Rahul','Oslo',5000)
insert into emp values(6,'e006','Harry','Puri',6000)
insert into emp values(7,'e007','Potter','Cuttack',7000)
insert into emp values(8,'e008','Tim','Kolkata',8000)
insert into emp values(9,'e009','Jack','Newyork',9000)
insert into emp values(10,'e010','Rohit','London',10000)
insert into emp values(11,'e011','Barry','Sydney',110000)
insert into emp values(12,'e012','Larry','Perth',120000)


<!--paging sample code for SQL server/Oracle/MySQL -->
<%@ page import="java.sql.*" %>
<%! int no_of_page,row_per_page=10,pages,start_row_count,end_row_count;String page_name="paging.jsp"; Connection con=null; %>

Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("Select count(*) from emp");
int total=0;


<table width="351" border="1">
      for(int i=1;i<=no_of_page;i++)
          out.println("<a href='"+page_name+"?pages="+i+"'>"+i+"</a>");

String pages1=request.getParameter("pages");
if(pages==0) pages=1;

String query="Select (Select count(*) from emp counter where counter.empno<=emp.empno) as rownumber,empno,ename,eaddress,sal from emp where (Select count(*) from emp counter where counter.empno<=emp.empno) between "+start_row_count+" and "+end_row_count +" order by empno";      
ResultSet rs1=stmt.executeQuery(query);

  for mysql
String query="Select * from emp limit "+start_row_count +",10";
<table width="351" border="1">
  <th width="79"><font color='#663300'>Row No </font></th>
  <th width="137"><font color='#663300'>Emp Number</font></th>
<th width="113"><font color='#663300'>Name</font></th>
<th width="113"><font color='#663300'>Address</font></th>
<th width="113"><font color='#663300'>Salary</font></th>
int rowno=0;
    rowno          =rs1.getInt("rownumber");    
    String eno     =rs1.getString("empno");
    String name    =rs1.getString("ename");
    String address =rs1.getString("eaddress");
    int  sal       =rs1.getInt("sal");

        <td><font color='red'><%=rowno%></font></td>
        <td><font color='red'><%=eno%></font></td>
        <td><font color='red'><%=name%></font></td>
        <td><font color='red'><%=address%></font></td>
        <td><font color='red'><%=sal%></font></td>

Running the application

Run the tomcat then write the below link in the Url

Here jsp is the Context path, which we mentioned in the server.xml file, which is present in (E:\Program Files\Apache Software Foundation\Tomcat 6.0\conf) directory.
After giving the url a set a listing will come, here only one appears
As paging.jsp.

 Subscribe to Articles


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend