Create Data Grid in JSP page

This is detailed code that how to create data grid that shows data from database table in jsp page.

Create Data Grid in JSP page

Create Data Grid in JSP page


This is detailed code that how to create data grid that shows data from database table in jsp page. In this jsp code given below we have used a tag library that provide some tags to create data grid. This code also provide facility to create paging and sorting, we can also specify number of data we want to see in one single page. This tag library provides some functions to calculate average and total.

First create table form where this data grid will fetch data and show in a jsp page. We have used database name 'mahendra' in this code and fetch data from table named 'employee_master' in same database.

Structure of table employee_master

CREATE TABLE `employee_master` (             
                   `emp_id` int(10) NOT NULL auto_increment,  
                   `firstName` varchar(20) default NULL,      
                   `lastname` varchar(20) default NULL,       
                   `salary` double default NULL,              
                   `bonus` double default NULL,               
                   PRIMARY KEY  (`emp_id`)                    


     <title>Example, Data Grid in jsp page</title>
    <%@ page contentType="text/html"%>
    <%@ page import="java.util.*, org.apache.taglibs.datagrid.
        DataGridParameters, java.sql.*" %>
    <%@ taglib uri="" 
        prefix="ui" %>
    <%@ taglib uri="" prefix="c" %>
    <%@ taglib uri="" prefix="fmt" %>
	<%! public class Employee {
	    protected String firstName;
	    protected String lastName;
	    protected double salary;
	    protected double bonus;
	    public String getFirstName () {
		return (firstName);
		     public String getLastName (){
			return (lastName);
		     public double getSalary () {
			return (salary);
		     public double getBonus () {
			return (bonus);
		      public void setFirstName (String newFirstName)
				firstName = newFirstName;
			  public void setLastName (String newLastName)
				lastName = newLastName;
			  public void setSalary (double newSalary)
				salary = newSalary;
			  public void setBonus (double newBonus)
				bonus = newBonus;
	<%  ArrayList employees = new ArrayList ();
	   Employee  objEmployee;
	     int fromIndex, toIndex;
	      try {
		String connectionURL = "jdbc:mysql://localhost:3306/mahendra";
	            Connection connection = null;
				Statement statement = null;
                ResultSet rs = null;
                connection = DriverManager.getConnection(connectionURL, 
                "root", "root");
                statement = connection.createStatement();
                String QueryString = "SELECT * from employee_master";
                rs = statement.executeQuery(QueryString);
			while ( {		
			objEmployee = new Employee ();
    catch (Exception ex) {
            System.out.println("Unable to connect to batabase."+ex);
   fromIndex = (int) DataGridParameters.getDataGridPageIndex (request, 
   if ((toIndex = fromIndex+4) >= employees.size ())
  toIndex = employees.size();
   request.setAttribute ("employees", employees.subList(fromIndex, toIndex));
    th a:link      { text-decoration: none; color: black }
     th a:visited   { text-decoration: none; color: black }
     .rows          { background-color: white }
     .hiliterows    { background-color: #848484; color: white; 
                      font-weight: bold }
     .alternaterows { background-color: #D8D8D8 }
     .header        { background-color: #2E2E2E; color: #D8D8D8;font-weight: 
                      bold }
	     .datagrid      { border: 1px solid #C7C5B2; font-family: arial; 
                      font-size: 9pt;
	    font-weight: normal }
   <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
   <ui:dataGrid items="${employees}" var="employee" name="datagrid1" 
    cellSpacing="0" styleClass="datagrid">
   <column width="50">
      <header value="Select" hAlign="center" styleClass="header"/>
        <input type="checkbox" align="center" name="employee" 
		    value="${employee.firstName} ${employee.lastName}"/>
      <aggregate function="count" var="total"/>
    <column width="150">
      <header value="Name" hAlign="center" styleClass="header"/>
      <item   value="${employee.firstName} ${employee.lastName}" hAlign="left" 
	     hyperLink="" hyperLinkTarget="_blank" 
      <aggregate function="count" var="total"/>
    <column width="200">
      <header value="Salary (INR)" hAlign="center" styleClass="header"/>
      <item   value="${employee.salary}" hAlign="right" pattern="#,##0.00 
         " styleClass="item"/>
      <aggregate function="avg" pattern="#,##0.00 " var="total"/>      
    <column width="200">
      <header value="Bonus (INR)" hAlign="center" styleClass="header"/>
      <item   value="${employee.bonus}" hAlign="right" pattern="#,##0.00 " 
      <aggregate function="avg" pattern="#,##0.00 " var="total"/>  
  <rows          styleClass="rows" hiliteStyleClass="hiliterows"/>
  <alternateRows styleClass="alternaterows"/>
  <paging        size="4" count="11" custom="true" nextUrlVar="next" 
       previousUrlVar="previous" pagesVar="pages"/>
  <order         imgAsc="up.gif" imgDesc="down.gif"/>
<table width="500" style="font-family: arial; font-size: 10pt">
<td align="left" width="33%">
<c:if test="${previous != null}">
<a href="<c:out value="${previous}"/>">Previous</a>
<td align="center" width="33%">
<c:forEach items="${pages}" var="page">
  <c:when test="${page.current}">
    <b><a href="<c:out value="${page.url}"/>">
                 <c:out value="${page.index}"/></a></b>
    <a href="<c:out value="${page.url}"/>"><c:out value="${page.index}"/></a>
<td align="right" width="33%">&nbsp;
<c:if test="${next != null}">
<a href="<c:out value="${next}"/>">Next</a>

When user clicks on link 'next' this shows next four data from the table......

Download Source Code