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); }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); } } 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(""); out.println(""); while (rs.next()) { String nombre = rs.getString("nombre"); String apellido = rs.getString("apellido"); int cantidad = rs.getInt("Cantidad_factura"); out.println("

NOMBRE = \\" + nombre + "

"); out.println("

APELLIDO = \\" + apellido + "

"); out.println("

CANTIDAD FACTURA = \\" + cantidad + "

"); } out.println(""); out.println(""); 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(""); out.println(""); out.print("Moneda " + "- " + "Cantidad
"); while (rs.next()) { String nombre = rs.getString("nombre"); String cantidad = rs.getString("Cantidad"); out.print("
" + nombre + " = \t"+cantidad+"
"); } out.println(""); out.println(""); rs.close(); stmt.close(); } catch (Exception e) { System.err.println(e.getClass().getName() + ": " + e.getMessage()); } } 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(""); out.println(""); out.print("

*** Nombre " + "- " + "Id - Gasto ***


"); while (rs.next()) { String nombre = rs.getString("nombre"); String clienteid = rs.getString("cliente_id"); String gasto = rs.getString("Gasto"); out.println(""); } out.println(""); out.println(""); 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(""); out.println(""); out.print("

*** Nombre Producto " + "- " + "Cantidad ***


"); while (rs.next()) { String nombre = rs.getString("nombre"); String cantidad = rs.getString("Cantidad"); out.println(""); } out.println(""); out.println(""); 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(""); out.println(""); out.print("

*** Nombre Producto " + "- " +"Id - " + "Cantidad ***


"); while (rs.next()) { String nombre = rs.getString("nombre"); String id = rs.getString("producto_id"); String cantidad = rs.getString("cantidad_Total"); out.println(""); } out.println(""); out.println(""); 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(""); out.println(""); out.print("

*** Nombre Producto " + "- " +"Id - " + "Cantidad ***


"); while (rs.next()) { String nombre = rs.getString("nombre"); String id = rs.getString("producto_id"); String cantidad = rs.getString("cantidad_Total"); out.println(""); } out.println(""); out.println(""); 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(""); out.println(""); out.print("

*** Nombre - Apellido - Fecha - Nombre Producto - " +"Cantidad - Cuenta ***


"); 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(""); } out.println(""); out.println(""); 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(""); out.println(""); out.print("

*** Nombre - Tipo Factura - Monto ***


"); while (rs.next()) { String nombre = rs.getString("nombre"); String factura = rs.getString("Tipo_Factura"); String monto = rs.getString("Monto"); out.println(""); } out.println(""); out.println(""); 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(""); out.println(""); out.print("

*** Nombre - Tipo Factura - Monto ***


"); while (rs.next()) { String nombre = rs.getString("nombre"); String factura = rs.getString("Tipo_Factura"); String monto = rs.getString("Monto"); out.println(""); } out.println(""); out.println(""); rs.close(); stmt.close(); } catch (Exception e) { System.err.println(e.getClass().getName() + ": " + e.getMessage()); } } public void destroy() { try { BD.getConnection().close(); } catch (SQLException e) { e.printStackTrace(); } } }