Commit f2fbe641 by Jose Baez

Initial commit

parents
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
<component name="libraryTable">
<library name="postgresql-42.3.4">
<CLASSES>
<root url="jar://$PROJECT_DIR$/lib/postgresql-42.3.4.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</component>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="17" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/jdbc.iml" filepath="$PROJECT_DIR$/jdbc.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
{
"java.project.sourcePaths": ["src"],
"java.project.outputPath": "bin",
"java.project.referencedLibraries": [
"lib/**/*.jar"
]
}
## Getting Started
Welcome to the VS Code Java world. Here is a guideline to help you get started to write Java code in Visual Studio Code.
## Folder Structure
The workspace contains two folders by default, where:
- `src`: the folder to maintain sources
- `lib`: the folder to maintain dependencies
Meanwhile, the compiled output files will be generated in the `bin` folder by default.
> If you want to customize the folder structure, open `.vscode/settings.json` and update the related settings there.
## Dependency Management
The `JAVA PROJECTS` view allows you to manage your dependencies. More details can be found [here](https://github.com/microsoft/vscode-java-dependency#manage-dependencies).
File added
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module-library" exported="">
<library>
<CLASSES>
<root url="jar://$MODULE_DIR$/../../Program Files (x86)/PostgreSQL/pgJDBC/postgresql-42.2.18.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
</component>
</module>
\ No newline at end of file
public class App {
public static void main(String args[]) {
Consulta1 c1= new Consulta1();
// Consultas
// c1.clientesMasFacturas();
// c1.clientesMasGastaron();
// c1.monedasMasUtilizadas();
// c1.topProveedorProductos();
// c1.productosMasVendidos();
// c1.productosMenosVendidos();
// c1.consultaNro7();
// c1.facturasSegunTotales();
c1.facturasSegunTotalesMasIVa();
}
}
\ No newline at end of file
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Consulta1 {
Connection c = null;
Statement stmt = null;
public void clientesMasFacturas( ) {
try {
Class.forName("org.postgresql.Driver");
c = DriverManager
.getConnection("jdbc:postgresql://localhost:5432/bootcamp_market",
"postgres", "Joserba84");
c.setAutoCommit(false);
System.out.println("* Top clientes con más facturas");
stmt = c.createStatement();
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;" );
while ( rs.next() ) {
String nombre = rs.getString("nombre");
String apellido = rs.getString("apellido");
int cantidad = rs.getInt("Cantidad_factura");
System.out.println( "NOMBRE = " + nombre );
System.out.println( "APELLIDO = " + apellido );
System.out.println( "CANTIDAD FACTURA = " + cantidad );
System.out.println();
}
rs.close();
stmt.close();
c.close();
} catch ( Exception e ) {
System.err.println( e.getClass().getName()+": "+ e.getMessage() );
System.exit(0);
}
System.out.println("Operation done successfully");
}
public void monedasMasUtilizadas( ) {
try {
Class.forName("org.postgresql.Driver");
c = DriverManager
.getConnection("jdbc:postgresql://localhost:5432/bootcamp_market",
"postgres", "Joserba84");
c.setAutoCommit(false);
System.out.println("* Top monedas más utilizadas");
stmt = c.createStatement();
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" );
while ( rs.next() ) {
String nombre = rs.getString("nombre");
int cantidad = rs.getInt("Cantidad");
System.out.println( "MONEDA = " + nombre );
System.out.println( "CANTIDAD = " + cantidad );
System.out.println();
}
rs.close();
stmt.close();
c.close();
} catch ( Exception e ) {
System.err.println( e.getClass().getName()+": "+ e.getMessage() );
System.exit(0);
}
System.out.println("Operation done successfully");
}
public void clientesMasGastaron( ) {
try {
Class.forName("org.postgresql.Driver");
c = DriverManager
.getConnection("jdbc:postgresql://localhost:5432/bootcamp_market",
"postgres", "Joserba84");
c.setAutoCommit(false);
System.out.println("* Top clientes que más gastaron");
stmt = c.createStatement();
ResultSet rs = stmt.executeQuery( "select f.nombre, f.apellido, 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 f.nombre, f.apellido\n" +
"order by Gasto desc;" );
while ( rs.next() ) {
String nombre = rs.getString("nombre");
String apellido = rs.getString("apellido");
int cantidad = rs.getInt("Gasto");
System.out.println( "NOMBRE = " + nombre );
System.out.println( "APELLIDO = " + apellido );
System.out.println( "MAYOR GASTO = " + cantidad );
System.out.println();
}
rs.close();
stmt.close();
c.close();
} catch ( Exception e ) {
System.err.println( e.getClass().getName()+": "+ e.getMessage() );
System.exit(0);
}
System.out.println("Operation done successfully");
}
public void topProveedorProductos( ) {
try {
Class.forName("org.postgresql.Driver");
c = DriverManager
.getConnection("jdbc:postgresql://localhost:5432/bootcamp_market",
"postgres", "Joserba84");
c.setAutoCommit(false);
System.out.println("* Top proveedor de productos");
stmt = c.createStatement();
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;" );
while ( rs.next() ) {
String nombre = rs.getString("nombre");
int cantidad = rs.getInt("Cantidad");
System.out.println( "PROVEEDOR = " + nombre );
System.out.println( "MAS VECES = " + cantidad );
System.out.println();
}
rs.close();
stmt.close();
c.close();
} catch ( Exception e ) {
System.err.println( e.getClass().getName()+": "+ e.getMessage() );
System.exit(0);
}
System.out.println("Operation done successfully");
}
public void productosMasVendidos() {
try {
Class.forName("org.postgresql.Driver");
c = DriverManager
.getConnection("jdbc:postgresql://localhost:5432/bootcamp_market",
"postgres", "Joserba84");
c.setAutoCommit(false);
System.out.println("* Productos más vendidos");
stmt = c.createStatement();
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;" );
while ( rs.next() ) {
String nombre = rs.getString("nombre");
String id = rs.getString("producto_id");
int cantidad = rs.getInt("cantidad_Total");
System.out.println( "NOMBRE = " + nombre );
System.out.println( "ID = " + id );
System.out.println( "CANTIDAD TOTAL = " + cantidad );
System.out.println();
}
rs.close();
stmt.close();
c.close();
} catch ( Exception e ) {
System.err.println( e.getClass().getName()+": "+ e.getMessage() );
System.exit(0);
}
System.out.println("Operation done successfully");
}
public void productosMenosVendidos() {
try {
Class.forName("org.postgresql.Driver");
c = DriverManager
.getConnection("jdbc:postgresql://localhost:5432/bootcamp_market",
"postgres", "Joserba84");
c.setAutoCommit(false);
System.out.println("* Productos menos vendidos");
stmt = c.createStatement();
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 asc limit 10;" );
while ( rs.next() ) {
String nombre = rs.getString("nombre");
String id = rs.getString("producto_id");
int cantidad = rs.getInt("cantidad_Total");
System.out.println( "NOMBRE = " + nombre );
System.out.println( "ID = " + id );
System.out.println( "CANTIDAD TOTAL = " + cantidad );
System.out.println();
}
rs.close();
stmt.close();
c.close();
} catch ( Exception e ) {
System.err.println( e.getClass().getName()+": "+ e.getMessage() );
System.exit(0);
}
System.out.println("Operation done successfully");
}
public void consultaNro7() {
try {
Class.forName("org.postgresql.Driver");
c = DriverManager
.getConnection("jdbc:postgresql://localhost:5432/bootcamp_market",
"postgres", "Joserba84");
c.setAutoCommit(false);
System.out.println("* Consulta que muestre fecha de emision de factura, nombre y apellido de cliente, \n" +
"* nombres de productos de esa factura, cantidades compradas, nombre de tipo de factura de una factura especfica");
stmt = c.createStatement();
ResultSet rs = stmt.executeQuery( "select a.nombre, a.apellido, b.fecha_emision, d.nombre as Producto, c.cantidad, e.nombre as Tipo_factura 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" );
while ( rs.next() ) {
String nombre = rs.getString("nombre");
String apellido = rs.getString("apellido");
String fecha = rs.getString("fecha_emision");
String producto = rs.getString("producto");
String cantidad = rs.getString("cantidad");
String factura = rs.getString("Tipo_factura");
System.out.println( "NOMBRE = " + nombre );
System.out.println( "APELLIDO = " + apellido );
System.out.println( "FECHA FACTURA = " + fecha );
System.out.println( "PRODUCTO = " + producto );
System.out.println( "CANTIDAD = " + cantidad );
System.out.println( "TIPO FACTURA = " + factura );
System.out.println();
}
rs.close();
stmt.close();
c.close();
} catch ( Exception e ) {
System.err.println( e.getClass().getName()+": "+ e.getMessage() );
System.exit(0);
}
System.out.println("Operation done successfully");
}
public void facturasSegunTotales() {
try {
Class.forName("org.postgresql.Driver");
c = DriverManager
.getConnection("jdbc:postgresql://localhost:5432/bootcamp_market",
"postgres", "Joserba84");
c.setAutoCommit(false);
System.out.println("* Montos de facturas ordenadas segun totales");
stmt = c.createStatement();
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" );
while ( rs.next() ) {
String nombre = rs.getString("nombre");
String tipoFactura = rs.getString("Tipo_Factura");
String monto = rs.getString("Monto");
System.out.println( "NOMBRE = " + nombre );
System.out.println( "TIPO FACTURA = " + tipoFactura );
System.out.println( "Monto = " + monto );
System.out.println();
}
rs.close();
stmt.close();
c.close();
} catch ( Exception e ) {
System.err.println( e.getClass().getName()+": "+ e.getMessage() );
System.exit(0);
}
System.out.println("Operation done successfully");
}
public void facturasSegunTotalesMasIVa() {
try {
Class.forName("org.postgresql.Driver");
c = DriverManager
.getConnection("jdbc:postgresql://localhost:5432/bootcamp_market",
"postgres", "Joserba84");
c.setAutoCommit(false);
System.out.println("* Mostrar el iva 10% de los montos totales de facturas (suponer que todos los productos tienen IVA 10%)");
stmt = c.createStatement();
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" );
while ( rs.next() ) {
String nombre = rs.getString("nombre");
String tipoFactura = rs.getString("Tipo_Factura");
String monto = rs.getString("Monto");
System.out.println( "NOMBRE = " + nombre );
System.out.println( "TIPO FACTURA = " + tipoFactura );
System.out.println( "Monto = " + monto );
System.out.println();
}
rs.close();
stmt.close();
c.close();
} catch ( Exception e ) {
System.err.println( e.getClass().getName()+": "+ e.getMessage() );
System.exit(0);
}
System.out.println("Operation done successfully");
}
}
public class Postgresql {
public static String conexionUrl="jdbc:postgresql://localhost:5432/";
public static String nameDataBase="bootcamp_market";
public static String userDatabase="postgres";
public static String passDatabase="Joserba84";
}
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