Commit 8f3f5128 by Josebaezx

se crea consultas de prueba

parent bb365c60
...@@ -2,6 +2,5 @@ ...@@ -2,6 +2,5 @@
<project version="4"> <project version="4">
<component name="Encoding"> <component name="Encoding">
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" /> <file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
</component> </component>
</project> </project>
\ No newline at end of file
...@@ -8,5 +8,5 @@ ...@@ -8,5 +8,5 @@
</list> </list>
</option> </option>
</component> </component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="liberica-1.8" project-jdk-type="JavaSDK" /> <component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK" />
</project> </project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RunConfigurationProducerService">
<option name="ignoredProducers">
<set>
<option value="com.android.tools.idea.compose.preview.runconfiguration.ComposePreviewRunConfigurationProducer" />
</set>
</option>
</component>
</project>
\ No newline at end of file
...@@ -8,63 +8,23 @@ import jakarta.servlet.http.HttpServletResponse; ...@@ -8,63 +8,23 @@ import jakarta.servlet.http.HttpServletResponse;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.sql.*; import java.sql.*;
@WebServlet("/conexion") public class BD {
public class BD extends HttpServlet {
Connection connection; public static Connection getConnection(){
public void init() { Connection con=null;
try { try{
Class.forName("org.postgresql.Driver"); Class.forName("org.postgresql.Driver");
connection = DriverManager con=DriverManager
.getConnection("jdbc:postgresql://localhost:5432/bootcamp_market", .getConnection("jdbc:postgresql://localhost:5432/bootcamp_market",
"postgres", "postgres"); "postgres", "Joserba84");
} catch (Exception e) { }catch(Exception e){
e.printStackTrace(); e.printStackTrace();
System.err.println(e.getClass().getName()+": "+e.getMessage()); System.err.println(e.getClass().getName()+": "+e.getMessage());
System.exit(0); System.exit(0);
} }
} return con;
@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("<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());
}
} }
public void destroy() {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
} }
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);
}
}
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());
}
}
public void destroy() {
try {
BD.getConnection().close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
<html> <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Home</title>
</head>
<body> <body>
<h2>Anda a alguna URL</h2> <h1>Bienvenido</h1>
<hr>
<form action="/servlets/consultas" method="get">
<input type="hidden" name="consulta" value="consulta1">
<button type="submit">Consulta 1</button>
</form><br>
<form action="/servlets/consultas" method="get">
<input type="hidden" name="consulta" value="consulta2">
<button type="submit">Consulta 2</button>
</form><br>
</body> </body>
</html> </html>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment