Forum

November 2nd, 2014
A A A
Avatar

Lost password?
Advanced Search

— Forum Scope —




— Match —





— Forum Options —





Minimum search word length is 3 characters - maximum search word length is 84 characters

The forums are currently locked and only available for read only access
sp_Feed Topic RSS sp_TopicIcon
how to load data from servlet using mysql into jqgrid
03/09/2012
16:54
Avatar
vikas
India
New Member
Members
Forum Posts: 1
Member Since:
03/09/2012
sp_UserOfflineSmall Offline

hello sir,

               I worked with simplest code of generating grid and it worked now i want to

1.load data from database in mysql

2. Add add,edit,delete functionality in the grid.

Sir i have to use jsp and servlet for my task.

plz sir help me in this .

Thanx in advance...

Below is my codes..

jqgrid.jsp

<!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=UTF-8">
        <title>JQGrid Special</title>                
        <script src="vikas/jquery-1.4.3.min.js"  type="text/javascript"></script>
        <script src="vikas/grid.locale-en.js"  type="text/javascript"></script>
        <script src="vikas/jquery.jqGrid.min.js" type="text/javascript"></script>
        <link href="vikas/jquery-ui-1.8.6.custom.css" rel="stylesheet" type="text/css" />
        <link href="vikas/ui.jqgrid.css" rel="stylesheet" type="text/css" />
        <link href="vikas/ui.multiselect.css" rel="stylesheet" type="text/css" />
        <script>
            function fillGridOnEvent(){
                $("#jQGrid").html("<table id="list"></table><div id="page"></div>");
                jQuery("#list").jqGrid({
                    url:'<%=request.getContextPath()%>/JQGridServlet?q=1&action=fetchData',
                    datatype: "xml",
                    height: 250,
                    colNames:['Sr. No.','Student Name','Student Std.','Student RollNo.'],
                    colModel:[
                        {name:'srNo',index:'srNo', width:90,sortable:true},
                        {name:'stdName',index:'stdName', width:130,sortable:true},
                        {name:'stdStd',index:'stdStd', width:100,sortable:true},
                        {name:'stdRollNo',index:'stdRollNo', width:130,sortable:true},
                        
                    ],
                    multiselect: false,
                    paging: true,
                    rowNum:10,
                    rowList:[10,20,30],
                    pager: $("#page"),
                    loadonce:true,
                    caption: "Student Details"
                }).navGrid('#page',{edit:false,add:false,del:false});
            }
            jQuery().ready(function (){
                //fillGrid();
            });
        </script>
    </head>
    <body onload="fillGridOnEvent();">
        <div id="jQGrid" align="center">
        </div>
    </body>
</html>

and JQGridServlet.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class JQGridServlet extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        try {

            if (request.getParameter("action").equals("fetchData")) {
                response.setContentType("text/xml;charset=UTF-8");

                String status = request.getParameter("status");

                String rows = request.getParameter("rows");
                String page = request.getParameter("page");

                int totalPages = 0;
                int totalCount = 15;

                if (totalCount > 0) {
                    if (totalCount % Integer.parseInt(rows) == 0) {
                        totalPages = totalCount / Integer.parseInt(rows);
                    } else {
                        totalPages = (totalCount / Integer.parseInt(rows)) + 1;
                    }

                } else {
                    totalPages = 0;
                }
                out.print("<?xml version='1.0' encoding='utf-8'?>\n");
                out.print("<rows>");
                out.print("<page>" + request.getParameter("page") + "</page>");

                out.print("<total>" + totalPages + "</total>");
                out.print("<records>" + 15 + "</records>");
                int srNo = 1;

                for (int i = 0; i < 15; i++) {
                    out.print("<row id='" + i + "'>");
                    out.print("<cell>" + srNo + "</cell>");
                    out.print("<cell>Taher</cell>");
                    out.print("<cell>8th</cell>");
                    out.print("<cell>25</cell>");
                    out.print("<cell><![CDATA[<a href='ViewStd.jsp'>View</a>]]></cell>");
                    out.print("</row>");
                    srNo++;
                }
                out.print("</rows>");
            }

        } finally {
            out.close();
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }
}

Forum Timezone: Europe/Sofia

Most Users Ever Online: 715

Currently Online:
42 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

OlegK: 1255

markw65: 179

kobruleht: 144

phicarre: 132

YamilBracho: 124

Renso: 118

Member Stats:

Guest Posters: 447

Members: 11373

Moderators: 2

Admins: 1

Forum Stats:

Groups: 1

Forums: 8

Topics: 10592

Posts: 31289

Newest Members:

, razia, Prankie, psky, praveen neelam, greg.valainis@pa-tech.com

Moderators: tony: 7721, Rumen[Trirand]: 81

Administrators: admin: 66

Comments are closed.
Privacy Policy   Terms and Conditions   Contact Information