App.java 9.06 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 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 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
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class App {
   public static void main(String args[]) {
      Connection c = null;
      Statement testamento = null;
      Statement tabla_cliente = null;
      
      try {
         Class.forName("org.postgresql.Driver");
         c = DriverManager
            .getConnection("jdbc:postgresql://localhost:5433/bootcamp_market",
            "postgres", "postgres");

            testamento = c.createStatement();
            tabla_cliente = c.createStatement();
            ResultSet cliente = tabla_cliente.executeQuery("SELECT * FROM public.cliente");
            ResultSet rs = testamento.executeQuery("SELECT cliente_id, nombre, COUNT (cliente_id) AS total FROM  factura INNER JOIN cliente ON factura.cliente_id=cliente.id GROUP BY cliente_id, nombre ORDER BY total DESC");
         System.out.println("--------------------------------------------------------------------------------------------------------");
         
         //CLIENTE CON MAS FACTURAS
         System.out.println("*Cliente con mas facturas: ");
         while (rs.next()) {
            int cliente_id = rs.getInt("cliente_id");
            String nombre = rs.getString("nombre");
            int total = rs.getInt("total");
            System.out.println("id: " + cliente_id+" nombre: " + nombre + " total:" + total);
            break;
         }
         System.out.println("--------------------------------------------------------------------------------------------------------");

         //TOP CLIENTES QUE MAS GASTARON
         System.out.println("*Top clientes que mas gastaron: ");
         rs = testamento.executeQuery("SELECT cliente_id, cliente.nombre, TRUNC(SUM(cantidad * precio)) AS total_gastado FROM factura JOIN factura_detalle ON factura.id=factura_detalle.factura_id JOIN producto ON producto.id=factura_detalle.producto_id JOIN cliente ON factura.cliente_id=cliente.id GROUP BY cliente_id, cliente.nombre ORDER BY  total_gastado DEsc");
         while (rs.next()) {
            int cliente_id= rs.getInt("cliente_id");
            String nombre = rs.getString("nombre");
            int total = rs.getInt("total_gastado");
            System.out.println("id: "+cliente_id + " nombre: " + nombre + " total gastado: " + total);
            break;
         }
         System.out.println("--------------------------------------------------------------------------------------------------------");
         
         //TOP MONEDAS MAS UTILIZADAS
         System.out.println("*Top monedas mas utilizadas: ");
         rs = testamento.executeQuery("SELECT moneda_id, nombre, COUNT (moneda_id) AS total FROM factura INNER JOIN moneda ON factura.moneda_id = moneda.id GROUP BY moneda_id, nombre ORDER BY total DESC");
         while (rs.next()) {
            int moneda_id = rs.getInt("moneda_id");
            String nombre = rs.getString("nombre");
            int total = rs.getInt("total");
            System.out.println("id: " + moneda_id + "\nnombre: " + nombre + "\ntotal:" + total);
            break;
         }
         System.out.println("--------------------------------------------------------------------------------------------------------");

         //TOP PROVEEDOR DE PRODUCTOS
         System.out.println("*Top proveedor de productos: ");
         rs = testamento.executeQuery("SELECT proveedor_id, proveedor.nombre, COUNT (proveedor_id) AS total FROM producto JOIN proveedor ON producto.proveedor_id = proveedor.id GROUP BY proveedor_id ,proveedor.nombre ORDER BY total DESC");
         while (rs.next()) {
            int proveedor_id = rs.getInt("proveedor_id");
            String nombre = rs.getString("nombre");
            int total = rs.getInt("total");
            System.out.println("Id: " + proveedor_id + "\nNombre: " + nombre + "\nCantidad de productos que provee: " + total);
            break;
         }
         System.out.println("--------------------------------------------------------------------------------------------------------");

         //TOP PRODUCTOS MAS VENDIDOS
         System.out.println("*Top productos mas vendidos: ");
         rs = testamento.executeQuery("SELECT producto_id, producto.nombre, floor(SUM(cantidad)) AS total FROM factura_detalle JOIN producto ON producto.id=factura_detalle.producto_id GROUP BY producto_id, producto.nombre ORDER BY total DESC");
         while (rs.next()) {
            int producto_id = rs.getInt("producto_id");
            String nombre = rs.getString("nombre");
            int total = rs.getInt("total");
            System.out.println("Id: " + producto_id + "\nNombre: " + nombre + "\nCantidades vendidas: " + total);
            break;
         }
         System.out.println("--------------------------------------------------------------------------------------------------------");
         //TOP PRODUCTOS MENOS VENDIDOS
         System.out.println("*Top productos menos vendidos: ");
         rs = testamento.executeQuery("SELECT producto_id, producto.nombre, floor(SUM(cantidad)) AS total FROM factura_detalle JOIN producto ON producto.id=factura_detalle.producto_id GROUP BY producto_id, producto.nombre ORDER BY total DESC");
         while (rs.next()) {
            int producto_id = rs.getInt("producto_id");
            String nombre = rs.getString("nombre");
            int total = rs.getInt("total");
            System.out.println("Id: " + producto_id + "\nNombre: " + nombre + "\nCantidades vendidas: " + total);
            break;
         }
         System.out.println("--------------------------------------------------------------------------------------------------------");
         
         //DETALLES CON ID DE FACTURA ESPECIFICO
         System.out.println("*Detalles de factura: ");
         int id_factura_especifica = 5;
         rs = testamento.executeQuery("SELECT factura.fecha_emision, cliente.nombre AS nombre_cliente, cliente.apellido, producto.nombre AS producto, factura_detalle.cantidad,factura_tipo.nombre AS tipo_de_Factura FROM factura JOIN factura_detalle ON factura_detalle.factura_id=factura.id JOIN factura_tipo ON factura.factura_tipo_id=factura_tipo.id JOIN producto ON factura_detalle.producto_id=producto.id JOIN cliente ON factura.cliente_id=cliente.id WHERE factura.id = "+ id_factura_especifica);
         while (rs.next()) {
            Date fecha_emision = rs.getDate("fecha_emision");
            String cliente_nombre = rs.getString("nombre_cliente");
            String cliente_apellido = rs.getString("apellido");
            String producto = rs.getString("producto");
            int cantidad = rs.getInt("cantidad");
            String tipo_de_factura = rs.getString("tipo_de_factura");
            System.out.println("Fecha de emision: " + fecha_emision + "\nNombre: " + cliente_nombre + "\nApellido: " + cliente_apellido + "\nProducto: " + producto + "\nCantidad: "+ cantidad + "\nTipo de factura: "+ tipo_de_factura);
            break;
         }
         System.out.println("--------------------------------------------------------------------------------------------------------");

         //MONTOS DE FACTURAS ORDENADAS SEGUND TOTALES
         System.out.println("*Montos de facturas ordenadas segun totales: ");
         
         rs = testamento.executeQuery("SELECT factura_id, TRUNC(SUM(cantidad * precio)) AS total_monto FROM factura_detalle JOIN producto ON factura_detalle.producto_id = producto.id JOIN factura ON factura_detalle.factura_id = factura.id GROUP BY factura_id ORDER BY total_monto DESC");
         while (rs.next()) {
            int factura_id = rs.getInt("factura_id");
            int cantidad = rs.getInt("total_monto");
            System.out.println("Id factura: " + factura_id + "\nNombre: "  + "\nTotal: " + cantidad);
            break;
         }
         System.out.println("--------------------------------------------------------------------------------------------------------");
         //MOSTRAR EL IVA 10% DE LOS TOTALES DE MONTOS DE FACTURAS
         System.out.println("*Iva 10% del total de monto de cada fatura: ");
         
         rs = testamento.executeQuery("SELECT factura_id, TRUNC(SUM(cantidad * precio )* 0.1) AS total_monto FROM factura_detalle JOIN producto ON factura_detalle.producto_id = producto.id JOIN factura ON factura_detalle.factura_id = factura.id GROUP BY factura_id ORDER BY total_monto DESC");
         while (rs.next()) {
            int factura_id = rs.getInt("factura_id");
            int cantidad = rs.getInt("total_monto");
            System.out.println("Id factura: " + factura_id + "\nNombre: "  + "\nTotal iva 10% : " + cantidad);
            break;
         }
         System.out.println("--------------------------------------------------------------------------------------------------------");


































         rs.close();
         testamento.close();
         c.close();
         tabla_cliente.close();
         cliente.close();
      } catch (Exception e) {
         e.printStackTrace();
         System.err.println(e.getClass().getName()+": "+e.getMessage());
         System.exit(0);
      }
      System.out.println("Opened database successfully");
   }
}