Question: how to featch data form db?

Ads

 
 
 

Share on Google+Share on Google+

Mumbai
how to featch data form db?
2 Answer(s)      3 years and 11 months ago
Posted in : JDBC

how to featch data form db?

Ads
View Answers

May 22, 2013 at 9:26 AM


/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package com.tcs.ignite.app.servlets;

import com.tcs.ignite.app.dao.ProductDAO; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;

/** * * @author 495944 */ public class getData extends HttpServlet {

/**
 * 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 {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    try {
        /*
         * TODO output your page here. You may use following sample code.
         */
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Servlet getData</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Servlet getData at " + request.getContextPath() + "</h1>");
        out.println("</body>");
        out.println("</html>");
    } finally {
        out.close();
    }
}

// <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 {
    String action = request.getParameter("act");
    if ("all".equalsIgnoreCase(action)) {
        PrintWriter out = response.getWriter();
        response.setContentType("application/json");
        out.print(new ProductDAO().getAllCatBrandSize());
    } else if ("cat".equalsIgnoreCase(action)) {
        PrintWriter out = response.getWriter();
        response.setContentType("application/json");
        String vals = request.getParameter("cat");
        if ("".equalsIgnoreCase(vals)) {
            out.print(new ProductDAO().getAllProducts());
        } else {
            List<Integer> ids = new ArrayList<Integer>();
            for (String val : vals.split(",")) {
                ids.add(Integer.parseInt(val));
            }
            out.print(new ProductDAO().getAllProductsFromSelectedCategory(ids));
        }
    }

}

/**
 * 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>

}



May 22, 2013 at 9:31 AM


jsp

<%-- 
    Document   : TCSBLUE
    Created on : Jan 8, 2013, 1:54:03 PM
    Author     : ignite415
--%>

<[email protected] import="com.tcs.ignite.app.beans.User"%>
<[email protected] contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Online Mens Shoes.com</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" href="Bootstrap2.2/css/bootstrap.css" />
        <link rel="stylesheet" href="Bootstrap2.2/css/bootstrap-responsive.css" />
        <link rel="stylesheet" href="Bootstrap2.2/css/sticky.css" />
        <link rel="stylesheet" href="CSS/style.css" />
        <style>
            body{

                background: url('Bootstrap2.2/img/background-homenew.png');
                background-repeat: repeat-x;
                background-color: #E3E3E3;
            }
        </style>
        <!--[if IE]><link rel="stylesheet" type="text/css" href="Bootstrap2.2/css/ie.css" /><![endif]-->
        <style>
            input[type=checkbox]{
                margin-left: 50px;
            }
            .thumbnail img{
                width: 300px;
                height: 200px;
            }
        </style>
        <script type="text/javascript" src="jquery.js"></script>
        <script type="text/javascript" src="bootstrap/js/bootstrap.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function(){

                $.getJSON('get','act=all',function(data){
                    var cats = data.cat;
                    for(var i=0;i<cats.length;i++){
                        $('#category').find('.controls').append('<input type="checkbox" name="cat" value="'+cats[i].id+'">&nbsp;'+cats[i].name+'&nbsp;(<a href="#">'+cats[i].count+'</a>)<br>');
                    }


                    var products = data.products;
                    for(var i=0;i<products.length;i++){
                        if(i%3==0){
                        var li = '<li style="margin-left:-0%" class="span4">'
                            +'<div class="thumbnail">'
                            +'<img data-src="holder.js/300x200" alt="" src="products/'+products[i].path+'">'
                            +'<h4>'+products[i].name+'</h4>'
                            + '<p>'+products[i].desc+'</p>'
                        +'<p>'+products[i].price+'</p>'
                        +'<input type="button" class="btn btn-primary" value="add to cart"/>'+'</div></li>';
 //+'<input type="button"value="view details"/>'
                        +'<input type="button" class="btn btn-primary" value="add to cart"/>'+'</div></li>';
                        $('.thumbnails').append(li);
                        }else
                            {
                                var li = '<li style="margin-left:-0%"  class="span4">'
                            +'<div class="thumbnail">'
                            +'<img data-src="holder.js/300x200" alt="" src="products/'+products[i].path+'">'
                            +'<h4>'+products[i].name+'</h4>'
                            + '<p>'+products[i].desc+'</p>'
                            +'<p>'+products[i].price+'</p>'

                         //+'<input type="button"value="view details"/>'
                        +'<input type="button" class="btn btn-primary" value="add to cart"/>'+'</div></li>';
$('.thumbnails').append(li);

                            }
                    }
                    $('.collapse').collapse();
                });
                $('#category').on('change','input[type=checkbox]',function(){
                    var val = new Array();
                    $('#category').find('input[type=checkbox]:checked').each(function(){
                        val[val.length] = $(this).val();
                    });
                    $.getJSON('get','act=cat&cat='+val.toString(),function(data){
                        $('ul.thumbnails').html('');
                        for(var i=0;i<data.length;i++){

                            var li = '<li style="margin-left:-0%" class="span4">'
                                +'<div class="thumbnail">'
                                +'<img data-src="holder.js/300x200" alt="" src="products/'+data[i].path+'">'
                                +'<h4>'+data[i].name+'</h4>'
                                + '<p>'+data[i].desc+'</p>'
                                  //+'<input type="button" value="view details"/>'
                        +'<input type="button" class="btn btn-primary"value="add to cart"/>'+'</div></li>';
                            $('.thumbnails').append(li);


                        }
                    });
                });
            });
        </script>


    </head>
    <body>
        <div class="navbar navbar-fixed-top">

            <div class="navbar-inner">
                <div class="container"> 

                    <a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse-header" style="margin-top: 12px;">
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </a>
                    <a class="brand" href="#">
                            <div class="logo"><img src="images/logo.jpg"></div>
                        Stylish.com <BR>
                        <div id="indian"> India's Largest Online Shoe Store</div>
                    </a>
                   <div class="search" <form class="form-search">
                        <div class="input-append">    
                            </div>
                    </from> </div>

                    <div class=" nav-collapse nav-collapse-header collapse navbar-responsive-collapse">
                        <ul class="nav ">

                            <li><a href="home.jsp"  rel="tooltip" data-placement="bottom" data-original-title=""><i class="icon-home"></i>&nbsp;Home</a></li>

                            <li><a href="next2.jsp" rel="tooltip" data-placement="bottom" data-original-title=""><i class="icon-bullhorn"></i> &nbsp;Men's</a></li>
                            <li><a href="aboutus.jsp" rel="tooltip" data-placement="bottom" data-original-title=""><i class="icon-font"></i> &nbsp;About us</a></li>
                          <li><a href="#myfeedback" data-toggle="modal" rel="tooltip" data-placement="bottom" data-original-title="Sign in"><i class="icon-user"></i> &nbsp;Feedback</a></li>

                        </ul>

                        <ul class="nav pull-right">
                                  <%
                                    User user = (User) request.getSession().getAttribute("user");
                                    String msg = (String) request.getAttribute("msg");
                                    if (msg != null) {
                                        out.print("<script>alert('" + msg + "')</script>");
                                    }
                                    if (user == null) {
                                %>



                                <ul class="nav pull-right">
                                    <li><a href="#myModal" data-toggle="modal" rel="tooltip" data-placement="bottom" data-original-title="Sign in"><i class="icon-user"></i> &nbsp;Sign in</a></li>
                                    <li><a href="register.jsp" data-placement="bottom" data-original-title="Sign Up"><i class="icon-thumbs-up"></i> &nbsp;Sign Up</a></li>
                                    </ul>
                                <%} else {%>
                                <h4>Welcome <%= user.getName()%></h4><br>
                                <a href="Login1">Logout</a>
                                <%}%>
                             <li class="dropdown">


                            </li>
                        </ul>
                    </div><!-- /.nav-collapse -->
                </div>
            </div>
        </div>
        <div class="container">
            <div class="content">

                <div class="wrapper">
                    <div class="proper-content">
                        <div class="row">
                            <div class="span12">
                                <div class="row-fluid">
                                    <div class="well" styln-height:e="mi 640px; margin-bottom: 66px; margin-top: 71px; height: 1387px; width: 894px; margin-left: 95px;" >
                                   <div class="row-fluid">
            <div class="span12">
                <div class="span8 pull-right">
                    <div class="products">
                        <ul class="thumbnails">

                        </ul>
                    </div>
                </div>
                <div class="span4">
                    <div class="accordion" id="accordion1">
                        <div class="accordion-group">
                            <div class="accordion-heading">
                                <a href="#category" data-parent="#accordion1" class="accordion-toggle" data-toggle="collapse">Category</a>
                            </div>
                            <div class="accordion-body collapse in" id="category">
                                <div class="controls">

                                </div>
                            </div>
                        </div>

<!--                        <div class="accordion-group">
                            <div class="accordion-heading">
                                <a href="#size" data-parent="#accordion1" class="accordion-toggle" data-toggle="collapse">Size</a>
                            </div>
                            <div class="accordion-body collapse in" id="size">
                                <div class="controls">

                                </div>
                            </div>
                        </div>-->
                    </div>
                </div>
            </div>
        </div>
                                </div>
                            </div>
                        </div>
                    </div><!-- /.proper-content -->

                    <div class="push"></div>

                </div><!-- /.wrapper -->

                <div class="footer-wrapper">
                    <footer>
                      <div style="width: 901px; margin-top: -72px; margin-bottom: 5px; margin-left: 92px;" class="navbar-inner">

                            <div class="pull-left">
                                <small>&copy; 2010-2013 Tata Consultancy Services Limited. All Rights Reserved</small>
                                <br/>
                                <small>
                                    <a rel="nofollow" target="_blank" href="http://www.tcs.com/contact/Pages/default.aspx">Contact us</a>&nbsp;|&nbsp;
                                    <a rel="nofollow" target="_blank" href="http://www.tcs.com/disclaimer/Pages/default.aspx">Disclaimer</a>&nbsp;|&nbsp;
                                    <a rel="nofollow" target="_blank" href="http://www.tcs.com/privacy/Pages/default.aspx">Privacy Policy</a>
                                </small>
                                <br/>
                            </div>
                            <div class="pull-right">
                                <a target="_blank" style="cursor: pointer;" href="http://www.tcs.com"><img src="Bootstrap2.2/img/icon-tata-white.png"/></a>
                            </div>



                        </div>
                    </footer>
                </div>
            </div>
        </div>

         <!--Modals-->

        <!--Sign in-->

        <div id="myModal" class="modal hide fade in" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="false">
                <div class="modal-header">
                    <a class="pull-right" data-dismiss="modal" aria-hidden="true"><i class="icon-remove" style="cursor: pointer"></i></a>
                    <h3><i class="icon-th-large  pull-left" style="margin-right: 10px;margin-top: 8px"></i>sign In</h3>
                </div>
                <div class="modal-body">
                    <form id="fbForm2" class="form-horizontal cmxform" name="fbForm2" method="post" action='Login1'>

                        <div class="control-group">
                            <label class="control-label" for="inputEmail">Email:</label>
                            <div class="controls">
                                <input type="text" id="inputEmail" name="inputEmail" placeholder="Email" class="required email">
                            </div>
                        </div>
                        <div class="control-group" style="margin-bottom: 15px;">
                            <label class="control-label" for="inputPassword">Password:</label>
                            <div class="controls">
                                <input type="password" id="inputPassword1" name="inputPassword1" placeholder="Password" class="required" minlength="8" style="margin-bottom: 10px;">
                                <br>
                                <input type="checkbox"> Remember me

                            </div>
                        </div>
                        <div class="control-group">
                            <div class="controls">
                                <button id="signIn" type="submit" class="btn btn-primary" style="margin-right: 10px;">Sign In</button>
                                <!--<button class="btn" href="fbForm2">Sign Up</button>-->
                            </div>
                        </div>
                        <div class="control-group">
                            <div class="controls">

                            </div>
                        </div>
                        <div class="register"> <a href="register.jsp">  for new user </a></div>
                    </form>
                </div>

            </div>  

            <div class="modal-footer">

<div class="register"> <a href="register.jsp">  for new user </a> <div>
            </div>
        </div>
        <script src="JQuery/jQuery1.8.3.js"></script>
        <script src="Bootstrap2.2/js/bootstrap.js"></script>
        <script>
            $(function() {
                $('li a').tooltip({delay:{show: 750,  hide: 100}});
            });
        </script>
    </body>
</html>

Ads









Related Tutorials/Questions & Answers:
DB2
Advertisements
Tutorials   
Java Spring Hibernate Struts Training how to retrieve text and images from mysql database and show on html page using jsp servlet How to install Neo4j on Ubuntu 15.10? please help me in these prog MYSQL - mysql copy table to another table example by creating new table solution for mapping hibernate and pojos from net beans through database Connect GWT application with Oracle inventory System of Medical Store inventory System of Medical Store inventory System of Medical Store inventory System of Medical Store This code have created jTable that connected with a database.every cell of last column have a button.But this button doesn't work properly.Where is the error java program Java Database Program jdbc Database connection class in a live project ..?? What is JDBC architecture? Unable to Create DSN for excel to ms access importing data and edit that data Database Scrolling Buttons how to use foreign key in java How to delete content of a log file from linux terminal? JSP-Oracle connectivity Student Management System Java code for Saving Marathi (Indian local language ) in Mysql and retrieving the same crud operations with foreign key constraint. I really need help with this assignment question Please help me out Please Backup Failed Mysqldump Got Errno 28 On Write Upgrade MySQL 5.1 to 5.5 in cpanel mysqldump all databases com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'jpa.sequence' doesn't exist persistence xml class element persistence.xml for MySQL MySQL JDBC Driver pom dependency code Blob column jdk6 and oracle 8i connexion oracle 8i et jdk6 java [ cannot retrive date from sql ] why?? Order Entry System How to threow timeout exception when DB is not responding How to threow timeout exception when DB is not responding This is the employee form to add data to mysql database but I got error of NullPointerException, Is this write code please give me feedback as soon as possible make a method which takes user name & pwd as parameter & validate them against the database. If the corresponding user exist return true otherwise false. prabhakar validating credentials and displaying error message in login .jsp if not valid JDBC jdbc jdbc code register servlet vibhu connectiondetails from vibhu

Ads

 
Advertisement null

Ads