ConsultaClienteFactura.java 14.7 KB
Newer Older
Josebaezx committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
package com.roshka.bootcamp;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

@WebServlet("/consultas")
public class ConsultaClienteFactura extends HttpServlet {

    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse res) {
        String respuesta= req.getParameter("consulta");
        if(respuesta.equals("consulta1")){
            consulta1(res);
        }else if(respuesta.equals("consulta2")){
            consulta2(res);
24 25 26 27 28 29 30 31 32 33 34 35 36 37
        }else if(respuesta.equals("consulta3")){
            consulta3(res);
        }else if(respuesta.equals("consulta4")){
            consulta4(res);
        }else if(respuesta.equals("consulta5")){
            consulta5(res);
        }else if(respuesta.equals("consulta6")){
            consulta6(res);
        }else if(respuesta.equals("consulta7")){
            consulta7(res);
        }else if(respuesta.equals("consulta8")){
            consulta8(res);
        }else if(respuesta.equals("consulta9")){
            consulta9(res);
Josebaezx committed
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
        }
    }

    private void consulta1(HttpServletResponse res) {
        try {
            Statement stmt = BD.getConnection().createStatement();
            res.setContentType("text/html");
            PrintWriter out = res.getWriter();
            ResultSet rs = stmt
                    .executeQuery("select a.nombre, apellido, count(b.cliente_id) Cantidad_factura from cliente a " +
                            "inner join factura b " +
                            "on a.id=b.cliente_id " +
                            "group by a.nombre, a.apellido " +
                            "order by Cantidad_factura desc;");
            out.println("<html>");
            out.println("<body>");
            while (rs.next()) {
                String nombre = rs.getString("nombre");
                String apellido = rs.getString("apellido");
                int cantidad = rs.getInt("Cantidad_factura");

                out.println("<p>NOMBRE = \\" + nombre + "</p>");
                out.println("<p>APELLIDO = \\" + apellido + "</p>");
                out.println("<p>CANTIDAD FACTURA = \\" + cantidad + "</p>");

            }
            out.println("</body>");
            out.println("</html>");
            rs.close();
            stmt.close();
        } catch (Exception e) {
            System.err.println(e.getClass().getName() + ": " + e.getMessage());
        }
    }

    private void consulta2(HttpServletResponse res) {
        try {
            Statement stmt = BD.getConnection().createStatement();
            res.setContentType("text/html");
            PrintWriter out = res.getWriter();
            ResultSet rs = stmt
                    .executeQuery( "select b.nombre, count(moneda_id) Cantidad  from factura a\n" +
                            "inner join moneda b\n" +
                            "on b.id=a.moneda_id\n" +
                            "group by b.nombre, a.moneda_id\n" +
                            "order by cantidad desc" );
            out.println("<html>");
            out.println("<body>");
            out.print("<b>Moneda " + "- " + "Cantidad</b><hr>");
            while (rs.next()) {
                String nombre = rs.getString("nombre");
                String cantidad = rs.getString("Cantidad");
                out.print("<h5>" + nombre + " = \t"+cantidad+"</h5>");

            }
            out.println("</body>");
            out.println("</html>");
            rs.close();
            stmt.close();
        } catch (Exception e) {
            System.err.println(e.getClass().getName() + ": " + e.getMessage());
        }
    }

102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
    private void consulta3(HttpServletResponse res) {
        try {
            Statement stmt = BD.getConnection().createStatement();
            res.setContentType("text/html");
            PrintWriter out = res.getWriter();
            ResultSet rs = stmt
                    .executeQuery( "select f.nombre, e.cliente_id, cast(sum(c.cantidad*d.precio) as integer)  as Gasto\n" +
                            "from factura_detalle c\n" +
                            "inner join producto d\n" +
                            "\ton d.id=producto_id\n" +
                            "inner join factura e\n" +
                            "\ton c.id=e.id\n" +
                            "inner join Cliente f\n" +
                            "\ton f.id=e.cliente_id\n" +
                            "group by e.cliente_id, f.nombre\n" +
                            "order by Gasto desc;" );
            out.println("<html>");
            out.println("<body>");
            out.print("<H3>*** Nombre " + "- " + "Id - Gasto ***<H3><hr>");
            while (rs.next()) {
                String nombre = rs.getString("nombre");
                String clienteid = rs.getString("cliente_id");
                String gasto = rs.getString("Gasto");
                out.println("<ul>");
                out.print("<li>" + nombre + " - \t "+clienteid +" - "+gasto+"</li>");
                out.println("</ul>");

            }
            out.println("</body>");
            out.println("</html>");
            rs.close();
            stmt.close();
        } catch (Exception e) {
            System.err.println(e.getClass().getName() + ": " + e.getMessage());
        }
    }

    private void consulta4(HttpServletResponse res) {
        try {
            Statement stmt = BD.getConnection().createStatement();
            res.setContentType("text/html");
            PrintWriter out = res.getWriter();
            ResultSet rs = stmt
                    .executeQuery( "select b.nombre, count(proveedor_id) Cantidad  from producto a\n" +
                            "inner join proveedor b\n" +
                            "on b.id=a.proveedor_id\n" +
                            "group by b.nombre, a.proveedor_id\n" +
                            "order by cantidad desc" );
            out.println("<html>");
            out.println("<body>");
            out.print("<H3>*** Nombre Producto " + "- " + "Cantidad ***<H3><hr>");
            while (rs.next()) {
                String nombre = rs.getString("nombre");
                String cantidad = rs.getString("Cantidad");
                out.println("<ul>");
                out.print("<li>" + nombre + " ==> "+cantidad+"</li>");
                out.println("</ul>");

            }
            out.println("</body>");
            out.println("</html>");
            rs.close();
            stmt.close();
        } catch (Exception e) {
            System.err.println(e.getClass().getName() + ": " + e.getMessage());
        }
    }

    private void consulta5(HttpServletResponse res) {
        try {
            Statement stmt = BD.getConnection().createStatement();
            res.setContentType("text/html");
            PrintWriter out = res.getWriter();
            ResultSet rs = stmt
                    .executeQuery( "select b.nombre, a.producto_id, sum(cantidad) as cantidad_Total from factura_detalle a\n" +
                            "inner join producto b\n" +
                            "on b.id=a.producto_id\n" +
                            "group by producto_id,b.nombre\n" +
                            "ORDER BY cantidad_Total desc limit 10;" );
            out.println("<html>");
            out.println("<body>");
            out.print("<H3>*** Nombre Producto " + "- " +"Id - " + "Cantidad ***<H3><hr>");
            while (rs.next()) {
                String nombre = rs.getString("nombre");
                String id = rs.getString("producto_id");
                String cantidad = rs.getString("cantidad_Total");
                out.println("<ul>");
                out.print("<li>" + nombre + " - Id: " + id +" - "+cantidad+"</li>");
                out.println("</ul>");

            }
            out.println("</body>");
            out.println("</html>");
            rs.close();
            stmt.close();
        } catch (Exception e) {
            System.err.println(e.getClass().getName() + ": " + e.getMessage());
        }
    }

    private void consulta6(HttpServletResponse res) {
        try {
            Statement stmt = BD.getConnection().createStatement();
            res.setContentType("text/html");
            PrintWriter out = res.getWriter();
            ResultSet rs = stmt
                    .executeQuery( "select b.nombre, a.producto_id, sum(cantidad) as cantidad_Total from factura_detalle a\n" +
                            "inner join producto b\n" +
                            "on b.id=a.producto_id\n" +
                            "group by producto_id,b.nombre\n" +
                            "ORDER BY cantidad_Total limit 10;" );
            out.println("<html>");
            out.println("<body>");
            out.print("<H3>*** Nombre Producto " + "- " +"Id - " + "Cantidad ***<H3><hr>");
            while (rs.next()) {
                String nombre = rs.getString("nombre");
                String id = rs.getString("producto_id");
                String cantidad = rs.getString("cantidad_Total");
                out.println("<ul>");
                out.print("<li>" + nombre + " - Id: " + id +" - "+cantidad+"</li>");
                out.println("</ul>");

            }
            out.println("</body>");
            out.println("</html>");
            rs.close();
            stmt.close();
        } catch (Exception e) {
            System.err.println(e.getClass().getName() + ": " + e.getMessage());
        }
    }

    private void consulta7(HttpServletResponse res) {
        try {
            Statement stmt = BD.getConnection().createStatement();
            res.setContentType("text/html");
            PrintWriter out = res.getWriter();
            ResultSet rs = stmt
                    .executeQuery( "select a.nombre, a.apellido, b.fecha_emision, d.nombre as Nombre_producto, c.cantidad, e.nombre as Tipo_cuenta from cliente a\n" +
                            "inner join factura b on a.id=b.cliente_id\n" +
                            "inner join factura_detalle c on c.factura_id = b.id\n" +
                            "inner join producto d on d.id=c.producto_id\n" +
                            "inner join factura_tipo e on e.id=b.factura_tipo_id\n" +
                            "order by b.id" );
            out.println("<html>");
            out.println("<body>");
            out.print("<H3>*** Nombre - Apellido - Fecha - Nombre Producto - " +"Cantidad - Cuenta ***<H3><hr>");
            while (rs.next()) {
                String nombre = rs.getString("nombre");
                String apellido = rs.getString("apellido");
                String fecha = rs.getString("fecha_emision");
                String producto = rs.getString("Nombre_producto");
                String cantidad = rs.getString("cantidad");
                String cuenta = rs.getString("Tipo_cuenta");
                out.println("<ul>");
                out.print("<li>" + nombre + " " + apellido +" - "+ fecha + " - " + producto+ " - " + cantidad + " - " + cuenta + "</li>");
                out.println("</ul>");

            }
            out.println("</body>");
            out.println("</html>");
            rs.close();
            stmt.close();
        } catch (Exception e) {
            System.err.println(e.getClass().getName() + ": " + e.getMessage());
        }
    }

    private void consulta8(HttpServletResponse res) {
        try {
            Statement stmt = BD.getConnection().createStatement();
            res.setContentType("text/html");
            PrintWriter out = res.getWriter();
            ResultSet rs = stmt
                    .executeQuery( "select e.nombre, f.nombre as Tipo_Factura, sum(cantidad*precio) as Monto\n" +
                            "from factura a\n" +
                            "inner join Cliente e on e.id=a.cliente_id\n" +
                            "inner join factura_detalle b on a.id=b.factura_id\n" +
                            "inner join producto c on c.id=b.producto_id\n" +
                            "inner join factura_tipo f on f.id=a.factura_tipo_id\n" +
                            "group by a.id, e.nombre, Tipo_Factura\n" +
                            "order by Monto desc" );
            out.println("<html>");
            out.println("<body>");
            out.print("<H3>*** Nombre - Tipo Factura - Monto ***<H3><hr>");
            while (rs.next()) {
                String nombre = rs.getString("nombre");
                String factura = rs.getString("Tipo_Factura");
                String monto = rs.getString("Monto");
                out.println("<ul>");
                out.print("<li>" + nombre + " - " + factura +" - "+ monto + "</li>");
                out.println("</ul>");

            }
            out.println("</body>");
            out.println("</html>");
            rs.close();
            stmt.close();
        } catch (Exception e) {
            System.err.println(e.getClass().getName() + ": " + e.getMessage());
        }
    }

    private void consulta9(HttpServletResponse res) {
        try {
            Statement stmt = BD.getConnection().createStatement();
            res.setContentType("text/html");
            PrintWriter out = res.getWriter();
            ResultSet rs = stmt
                    .executeQuery( "select e.nombre, f.nombre as Tipo_Factura, sum(cantidad*precio)*0.1 as Monto\n" +
                            "from factura a\n" +
                            "inner join Cliente e on e.id=a.cliente_id\n" +
                            "inner join factura_detalle b on a.id=b.factura_id\n" +
                            "inner join producto c on c.id=b.producto_id\n" +
                            "inner join factura_tipo f on f.id=a.factura_tipo_id\n" +
                            "group by a.id, e.nombre, Tipo_Factura\n" +
                            "order by Monto desc" );
            out.println("<html>");
            out.println("<body>");
            out.print("<H3>*** Nombre - Tipo Factura - Monto ***<H3><hr>");
            while (rs.next()) {
                String nombre = rs.getString("nombre");
                String factura = rs.getString("Tipo_Factura");
                String monto = rs.getString("Monto");
                out.println("<ul>");
                out.print("<li>" + nombre + " - " + factura +" - "+ monto + "</li>");
                out.println("</ul>");

            }
            out.println("</body>");
            out.println("</html>");
            rs.close();
            stmt.close();
        } catch (Exception e) {
            System.err.println(e.getClass().getName() + ": " + e.getMessage());
        }
    }


Josebaezx committed
341 342 343 344 345 346 347 348
    public void destroy() {
        try {
            BD.getConnection().close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}