Wednesday, October 17, 2018

Java Server Pages (JSP) Displaying table data using pagination

 

Java Server Pages (JSP)


//Displaying list of employees for the given department 

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

<%
  int PAGESIZE = 5;
  
  String spageno = request.getParameter("pageno");
  int pageno, nopages;
  
  
  if ( spageno == null)
     pageno=1;
  else
     pageno = Integer.parseInt(spageno);
     

  // connect using Thin driver
  Class.forName("oracle.jdbc.driver.OracleDriver"); 
  Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:oracle8i","scott","tiger");
  Statement st = con.createStatement();
  ResultSet rs = st.executeQuery("select * from emp");
  
  // skip rows based on page number and also find out no. of rows 
  int srow = (pageno - 1) * PAGESIZE;
  
%>

<h2>Employees List </h2>
<table border=1>
<tr><th>Empno<th>Name <th>Salary
</tr>

<%
  int nrows = 0 ;
  while ( rs.next())
  {
     if ( nrows >= srow  &&  nrows  < srow + PAGESIZE)
         out.println( "<tr><td>" + rs.getString("empno") + "<td>" + rs.getString("ename") + "<td>" +  rs.getString("sal") + "</tr>" );
         
     nrows++;
  }
  
  rs.close();
  st.close();
  con.close();
    
  // convert double to int using ceil() function
  nopages = (int) Math.ceil(nrows / (double) PAGESIZE);
  
  // display page no.with hyper links
  out.println("<tr> <td colspan=3>");
  
  for ( int i = 1; i <= nopages ;i ++)
    if ( i != pageno )
      out.println("<a href=emplist.jsp?pageno=" + i + ">" +  i + "</a>&nbsp;");
    else
      out.println(i + "&nbsp;");
  
  out.println("</tr></table>");


%>


No comments:

Post a Comment

CORBA Java Tutorial using Netbeans and Java 8.

CORBA-Example A simple CORBA implementation using Java Echo.idl module EchoApp{ interface Echo{ string echoString(); }; }; ...