jquery jtable plugin example.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title></title>
<!-- Include one of jTable styles. -->
<link href="css/metro/crimson/jtable.css" rel="stylesheet" type="text/css" />
<link href="css/jquery-ui-1.10.3.custom.css" rel="stylesheet" type="text/css" />
<!-- Include jTable script file. -->
<script src="js/jquery-1.8.2.js" type="text/javascript"></script>
<script src="js/jquery-ui-1.10.3.custom.js" type="text/javascript"></script>
<script src="js/jquery.jtable.js" type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $('#PersonTableContainer').jtable({
            title: 'Table of people',
            paging:true,
            pageSize:10,
            actions: {
                listAction: 'Controller?action=list',
             
            },
            fields: {
                Customer_Name: {
                    title:'Customer_Name',
                    key: true,
                    list: true,
                    create:true
                },
                Product_Name: {
                    title: 'Product_Name',
                    width: '30%',
                    edit:false
                },
                Sale_Quantity: {
                    title: 'Sale_Quantity',
                    width: '30%',
                    edit:true
                },
                             
            }
        });
        $('#PersonTableContainer').jtable('load');
    });
</script>
</head>
<body>
<div style="width:60%;margin-right:20%;margin-left:20%;text-align:center;">

<div id="PersonTableContainer"></div>
</div>
</body>
</html>




selling.java

package Connect;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

public class selling
{
    public int ID;
    public String Bill_No;
    public String Bill_Date;
    public String Order_No;
    public String Customer_Name;
    public String Buyer_Tin;
    public String Order_Date;
    public String Product_Code;
    public String Bill_Type;
    public String Product_Name;
    public double Sell_Price;
    public int Sale_Quantity;
    public double Total;
    public double VAT;
    public double SAT;
    public double Round_Off;
    public double Grand_Total;
    public double SaleByAmount;
    public double Margin;

    public int getID() {
        return ID;
    }

   

    public String getBill_No() {
        return Bill_No;
    }

    public void setBill_No(String Bill_No) {
        this.Bill_No = Bill_No;
    }

    public String getBill_Date() {
        return Bill_Date;
    }

    public void setBill_Date(String Bill_Date) {
        this.Bill_Date = Bill_Date;
    }

    public String getOrder_No() {
        return Order_No;
    }

    public void setOrder_No(String Order_No) {
        this.Order_No = Order_No;
    }

    public String getCustomer_Name() {
        return Customer_Name;
    }

    public void setCustomer_Name(String Customer_Name) {
        this.Customer_Name = Customer_Name;
    }

    public String getBuyer_Tin() {
        return Buyer_Tin;
    }

    public void setBuyer_Tin(String Buyer_Tin) {
        this.Buyer_Tin = Buyer_Tin;
    }

    public String getOrder_Date() {
        return Order_Date;
    }

    public void setOrder_Date(String Order_Date) {
        this.Order_Date = Order_Date;
    }

    public String getProduct_Code() {
        return Product_Code;
    }

    public void setProduct_Code(String Product_Code) {
        this.Product_Code = Product_Code;
    }

    public String getBill_Type() {
        return Bill_Type;
    }

    public void setBill_Type(String Bill_Type) {
        this.Bill_Type = Bill_Type;
    }

    public String getProduct_Name() {
        return Product_Name;
    }

    public void setProduct_Name(String Product_Name) {
        this.Product_Name = Product_Name;
    }

    public double getSell_Price() {
        return Sell_Price;
    }

    public void setSell_Price(double Sell_Price) {
        this.Sell_Price = Sell_Price;
    }

    public int getSale_Quantity() {
        return Sale_Quantity;
    }

    public void setSale_Quantity(int Sale_Quantity) {
        this.Sale_Quantity = Sale_Quantity;
    }

    public double getTotal() {
        return Total;
    }

    public void setTotal(double Total) {
        this.Total = Total;
    }

    public double getVAT() {
        return VAT;
    }

    public void setVAT(double VAT) {
        this.VAT = VAT;
    }

    public double getSAT() {
        return SAT;
    }

    public void setSAT(double SAT) {
        this.SAT = SAT;
    }

    public double getRound_Off() {
        return Round_Off;
    }

    public void setRound_Off(double Round_Off) {
        this.Round_Off = Round_Off;
    }

    public double getGrand_Total() {
        return Grand_Total;
    }

    public void setGrand_Total(double Grand_Total) {
        this.Grand_Total = Grand_Total;
    }

    public double getSaleByAmount() {
        return SaleByAmount;
    }

    public void setSaleByAmount(double SaleByAmount) {
        this.SaleByAmount = SaleByAmount;
    }

    public double getMargin() {
        return Margin;
    }

    public void setMargin(double Margin) {
        this.Margin = Margin;
    }

    public selling(int ID, String Bill_No, String Bill_Date, String Order_No, String Customer_Name, String Buyer_Tin, String Order_Date, String Product_Code, String Bill_Type, String Product_Name, double Sell_Price, int Sale_Quantity, double Total, double VAT, double SAT, double Round_Off, double Grand_Total, double SaleByAmount, double Margin) {
        this.ID = ID;
        this.Bill_No = Bill_No;
        this.Bill_Date = Bill_Date;
        this.Order_No = Order_No;
        this.Customer_Name = Customer_Name;
        this.Buyer_Tin = Buyer_Tin;
        this.Order_Date = Order_Date;
        this.Product_Code = Product_Code;
        this.Bill_Type = Bill_Type;
        this.Product_Name = Product_Name;
        this.Sell_Price = Sell_Price;
        this.Sale_Quantity = Sale_Quantity;
        this.Total = Total;
        this.VAT = VAT;
        this.SAT = SAT;
        this.Round_Off = Round_Off;
        this.Grand_Total = Grand_Total;
        this.SaleByAmount = SaleByAmount;
        this.Margin = Margin;
    }

    public selling(String Customer_Name, String Product_Name, int Sale_Quantity) {
        this.Customer_Name = Customer_Name;
        this.Product_Name = Product_Name;
        this.Sale_Quantity = Sale_Quantity;
    }

    public selling() {
    }
public ArrayList list(int jtStartIndex, int jtPageSize)
{
    ArrayList ar=new ArrayList();
    try
    {
         String startIndex=Integer.toString(jtStartIndex);
         String pageSize=Integer.toString(jtPageSize);
         databse d=new databse();
         Connection con=d.connectit();
         String sql="select Customer_Name,Product_Name,Sale_Quantity from sales limit "+startIndex+","+pageSize+" ";
         PreparedStatement pst=con.prepareStatement(sql);
         ResultSet rs=pst.executeQuery();
         while(rs.next())
         {
             String name=rs.getString(1);
             String p=rs.getString(2);
             int q=rs.getInt(3);
           
             ar.add(new selling(name,p,q));
         }
         pst.close();
         con.close();
         rs.close();
               
                 }
    catch(Exception e)
    {
        System.out.println(e);
        e.printStackTrace();
    }
    return ar;
   
}
public ArrayList list1(String d1,String d2)
{
    ArrayList ar=new ArrayList();
    try
    {
         databse d=new databse();
         Connection con=d.connectit();
         String sql="SELECT sum(Total),sum(Sale_Quantity) from sales where Bill_Date between '"+d1+"' and '"+d2+"'";
         PreparedStatement pst=con.prepareStatement(sql);
         ResultSet rs=pst.executeQuery();
         while(rs.next())
         {
             double total=rs.getDouble(1);
             int sq=rs.getInt(2);
             ar.add(new selling(total,sq));
         }
         pst.close();
         con.close();
         rs.close();
               
                 }
    catch(Exception e)
    {
        System.out.println(e);
        e.printStackTrace();
    }
    return ar;
   
}
public ArrayList list2(String d1,String d2)
{
    ArrayList ar=new ArrayList();
    try
    {
         databse d=new databse();
         Connection con=d.connectit();
         String sql="SELECT Product_Code,Product_Name,Sale_Quantity as Qty_Sold,Sell_Price,Margin,SaleByAmount from sales where Bill_Date between '"+d1+"' and '"+d2+"'";
         PreparedStatement pst=con.prepareStatement(sql);
         ResultSet rs=pst.executeQuery();
         while(rs.next())
         {
             String code=rs.getString(1);
             String name=rs.getString(2);
             int sq=rs.getInt(3);
             double Sell_Price=rs.getDouble(4);
             double margin=rs.getDouble(5);
             double sa=rs.getDouble(6);
             ar.add(new selling(code,name,sq,Sell_Price,margin,sa));
         }
         pst.close();
         con.close();
         rs.close();
               
                 }
    catch(Exception e)
    {
        System.out.println(e);
        e.printStackTrace();
    }
    return ar;
   
}

    public selling(String Product_Code, String Product_Name,int Sale_Quantity, double Sell_Price, double SaleByAmount, double Margin) {
        this.Product_Code = Product_Code;
        this.Product_Name = Product_Name;
        this.Sell_Price = Sell_Price;
        this.Sale_Quantity = Sale_Quantity;
        this.SaleByAmount = SaleByAmount;
        this.Margin = Margin;
    }
    public selling( double Total,int Sale_Quantity) {
        this.Sale_Quantity = Sale_Quantity;
        this.Total = Total;
    }
    public int coun()
    {
        int c=0;
        try
        {
       databse d=new databse();
         Connection con=d.connectit();
         String sql= "SELECT count(*) from sales";
          PreparedStatement pst=con.prepareStatement(sql);
         ResultSet rs=pst.executeQuery();
         while(rs.next())
         {
            c=rs.getInt(1);
         }
         }
        catch(Exception e)
        {
            System.out.println(e);
        }
        return c;
    }
}


Controller.java
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Connect;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.reflect.TypeToken;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.json.JsonArray;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.registry.infomodel.User;

/**
 *
 * @author NAVJYOTI
 */
@WebServlet(name = "Controller", urlPatterns = {"/Controller"})
public class Controller extends HttpServlet {
selling s;
    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
       if(request.getParameter("action")!=null){
List<selling> lstUser=new ArrayList<selling>();
String action=(String)request.getParameter("action");
Gson gson = new Gson();
response.setContentType("application/json");
int startPageIndex= Integer.parseInt(request.getParameter("jtStartIndex"));
                        int numRecordsPerPage=Integer.parseInt(request.getParameter("jtPageSize"));
if(action.equals("list")){
try{
                                    s=new selling();
                                    int userCount=s.coun();
//Fetch Data from User Table
lstUser=s.list(startPageIndex,numRecordsPerPage);
//Convert Java Object to Json
JsonElement element = gson.toJsonTree(lstUser, new TypeToken<List<selling>>() {}.getType());
   com.google.gson.JsonArray jsonArray = element.getAsJsonArray();
String listData=jsonArray.toString();
//Return Json in the format required by jTable plugin
listData="{\"Result\":\"OK\",\"Records\":"+listData+",\"TotalRecordCount\":"+userCount+"}";  
response.getWriter().print(listData);
}catch(Exception ex){
String error="{\"Result\":\"ERROR\",\"Message\":"+ex.getMessage()+"}";
response.getWriter().print(error);
ex.printStackTrace();
}
}
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}

Popular posts from this blog

Simple Sign up design. Android.

Cart page design in android.

Set Date on jDateChooser and retrieve record from database.