package com.roshka.bootcamp; import jakarta.servlet.ServletConfig; import jakarta.servlet.ServletContext; 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.*; @WebServlet("/context") public class Context extends HttpServlet { Connection connection; public void init(ServletConfig config) { ServletContext context = config.getServletContext(); try { Class.forName("org.postgresql.Driver"); connection = DriverManager .getConnection(context.getInitParameter("dbUrl"), context.getInitParameter("dbUser"), context.getInitParameter("dbPassword")); } catch (Exception e) { e.printStackTrace(); System.err.println(e.getClass().getName() + ": " + e.getMessage()); System.exit(0); } } @Override public void doGet(HttpServletRequest req, HttpServletResponse res) { try { Statement stmt = connection.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("
"); 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()); } } public void destroy() { try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } } }