Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package proyectoyomusico;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.Scanner;
- /**
- *
- * @author Cristian
- */
- public class ProyectoYomusico {
- static Scanner teclado = new Scanner(System.in, "UTF-8");
- static com.mysql.jdbc.Connection con;
- static String sql;
- static Statement sentencia;
- static ResultSet rs;
- public static void main(String[] args) throws ClassNotFoundException, SQLException {
- int menu = 0;
- try {
- Class.forName("com.mysql.jdbc.Driver");
- String url = "jdbc:mysql://localhost/Yomusico";
- con = (com.mysql.jdbc.Connection) DriverManager.getConnection(url, "root", "");
- do {
- Integer op;
- System.out.println("-----------------------------------\n"
- + "||Menu Principal ||\n"
- + "-----------------------------------\n"
- + "|1.Empleados |\n"
- + "|2.Tiendas |\n"
- + "|3.Pedidos |\n"
- + "|4.Clientes |\n"
- + "|5.Instrumentos |\n"
- + "|0.Salir |\n"
- + "-----------------------------------");
- op = teclado.nextInt();
- switch (op) {
- case 1:
- empleados();
- break;
- case 2:
- tiendas();
- break;
- // case 3:
- // Pedidos();
- // break;
- // case 4:
- // Clientes();
- // break;
- // case 5:
- // Instrumentos();
- // break;
- case 0:
- System.out.println("Saliendo...\n\nHasta Pronto");
- break;
- default:
- System.out.println("Opcion no valida");
- }
- } while (menu != 2);
- con.close();
- } catch (ClassNotFoundException | SQLException ex) {
- System.err.println("Error: " + ex);
- }
- }
- static void empleados() throws SQLException {
- Integer op;
- do {
- System.out.println("-----------------------------------\n"
- + "||Menu Principal ||\n"
- + "-----------------------------------\n"
- + "|1.Mostrar Empleados |\n"
- + "|2.Añadir Empleados |\n"
- + "|3.Eliminar Empleados |\n"
- + "|4.Modificar Empleados |\n"
- + "|0.Volver |\n"
- + "-----------------------------------");
- op = teclado.nextInt();
- teclado.nextLine();
- switch (op) {
- case 1:
- mostrarEmpleados();
- break;
- case 2:
- añadirEmpleados();
- break;
- case 3:
- eliminarEmpleados();
- break;
- case 4:
- modificarEmpleados();
- case 0:
- System.out.println("Volviendo...\n");
- break;
- default:
- System.out.println("Opcion no valida");
- }
- } while (op != 0);
- }
- static void mostrarEmpleados() throws SQLException {
- Integer op;
- do {
- System.out.println("-----------------------------------\n"
- + "||Menu Modificación Empleados ||\n"
- + "-----------------------------------\n"
- + "|1.Mostrar Empleados: |\n"
- + "|2.Buscar por Tienda: |\n"
- + "|3.Busqueda por ventas de cantidad : |\n"
- + "|0.Volver. |\n"
- + "-----------------------------------");
- op = teclado.nextInt();
- teclado.nextLine();
- switch (op) {
- case 1:
- empleadosTodos();
- break;
- case 2:
- empleadosTiendas();
- break;
- case 3:
- empleadosVentas();
- break;
- case 0:
- System.out.println("Volviendo...\n");
- break;
- default:
- System.out.println("Opcion no valida");
- }
- } while (op != 0);
- }
- static void empleadosTodos() throws SQLException {
- try {
- System.out.println("Se ha establecido la conexión...");
- sql = "SELECT * FROM Empleados;";
- sentencia = sentencia = con.createStatement();
- rs = sentencia.executeQuery(sql);
- boolean todos = false;
- while (rs.next()) {
- String res = rs.getString("numemp") + "\t "
- + rs.getString("nombre") + "\t"
- + rs.getString("edad") + "\t"
- + rs.getString("tienda") + "\t"
- + rs.getString("ejerce") + "\t"
- + rs.getString("contrato") + "\t"
- + rs.getString("cuota") + "\t"
- + rs.getString("ventas");
- System.out.println(res);
- todos = true;
- }
- if (todos == true) {
- System.out.println("Empleados mostrados. Pulse para continuar!");
- String test = teclado.nextLine();
- } else {
- System.out.println("Los Empleados no han sido mostrado resultados. Pulse para continuar!");
- }
- } catch (SQLException ex) {
- System.err.println("Error: " + ex);
- }
- }
- static void empleadosTiendas() throws SQLException {
- try {
- System.out.println("Se ha establecido la conexión...");
- todasTiendas();
- System.out.println("Introduce la tienda a buscar(por numero): ");
- String codtienda = teclado.nextLine();
- sql = "SELECT * FROM Empleados where tienda= " + codtienda + ";";
- sentencia = sentencia = con.createStatement();
- rs = sentencia.executeQuery(sql);
- boolean tienda = false;
- while (rs.next()) {
- String res = rs.getString("numemp") + "\t "
- + rs.getString("nombre") + "\t"
- + rs.getString("edad") + "\t"
- + rs.getString("tienda") + "\t"
- + rs.getString("ejerce") + "\t"
- + rs.getString("contrato") + "\t"
- + rs.getString("cuota") + "\t"
- + rs.getString("ventas");
- System.out.println(res);
- tienda = true;
- }
- if (tienda == true) {
- System.out.println("Tienda encontrada. Pulse para continuar!");
- String test = teclado.nextLine();
- } else {
- System.out.println("La sobre Tienda no a mostrado resultados. Pulse para continuar!");
- }
- } catch (SQLException ex) {
- System.err.println("Error: " + ex);
- }
- }
- static void empleadosVentas() throws SQLException {
- try {
- System.out.println("Se ha establecido la conexión...");
- System.out.println("Introduce la cantidad de ventas de mayor a menor: ");
- String pre = teclado.nextLine();
- sql = "SELECT * FROM Empleados where ventas <= " + pre + "";
- sentencia = con.createStatement();
- rs = sentencia.executeQuery(sql);
- boolean salida = false;
- while (rs.next()) {
- String res = rs.getString("numemp") + "\t "
- + rs.getString("nombre") + "\t"
- + rs.getString("edad") + "\t"
- + rs.getString("tienda") + "\t"
- + rs.getString("ejerce") + "\t"
- + rs.getString("contrato") + "\t"
- + rs.getString("cuota") + "\t"
- + rs.getString("ventas");
- System.out.println(res);
- salida = true;
- }
- if (salida == true) {
- System.out.println("Cantidad encontrada. Pulse para continuar!");
- String test = teclado.nextLine();
- } else {
- System.out.println("La consulta no ha mostrado resultado. Pulse para continuar!");
- }
- } catch (SQLException ex) {
- System.err.println("Error: " + ex);
- }
- }
- static boolean existeEmpleados(int num) {
- boolean existe = false;
- sql = "SELECT * FROM Empleados WHERE numemp=" + num + ";";
- try {
- sentencia = con.createStatement();
- rs = sentencia.executeQuery(sql);
- if (rs.next()) {
- existe = true;
- } else {
- existe = false;
- }
- } catch (SQLException ex) {
- }
- return existe;
- }
- static void añadirEmpleados() throws SQLException {
- System.out.println("Introduce el numemp (número id): ");
- int num = teclado.nextInt();
- if (!existeEmpleados(num)) {
- System.out.println("Introduce el Nombre del empleado: ");
- String nom = teclado.nextLine();
- System.out.println("Introduce la edad del empleado: ");
- String edad = teclado.nextLine();
- System.out.println("Introduce la tienda donde trabaja el Empleado a introducir(numero de la tienda.): ");
- String tien = teclado.nextLine();
- System.out.println("Introduce el trabajo que realiza el empleado: ");
- String ejer = teclado.nextLine();
- System.out.println("Introduce la fecha del contrato de cuando se realizó(AÑO-MES-DIA): ");
- String contrato = teclado.nextLine();
- System.out.println("Introduce la cuota de dicho Empleado: ");
- String cuota = teclado.nextLine();
- System.out.println("Introduce las ventas realizada por el Empleado: ");
- String ventas = teclado.nextLine();
- sql = "INSERT INTO Empleados VALUE ('" + num + "','" + nom + "','" + edad + "','" + tien + "','" + ejer + "','" + contrato + "' ,'" + cuota + "','" + ventas + "');";
- sentencia = con.createStatement();
- sentencia.executeUpdate(sql);
- } else {
- System.out.println("El numemp" + num + "Ya existe. Pruebe a introducir otro.");
- }
- }
- static void eliminarEmpleados() throws SQLException {
- // BORRA UN EMPLEADO
- empleadosTodos();
- System.out.println("Introducir el numemp del Empleado para borrar: ");
- String numemp = teclado.nextLine();
- sql = "Delete From Empleados Where numemp = '" + numemp + "';";
- sentencia = con.createStatement();
- sentencia.executeUpdate(sql);
- System.out.println("Empleado Borrado! Pulse para continuar!");
- String test = teclado.nextLine();
- }
- static void modificarEmpleados() throws SQLException {
- // // MODIFICA UN Empleado
- Integer op;
- do {
- System.out.println("-----------------------------------\n"
- + "||Menu Modificación Empleados ||\n"
- + "-----------------------------------\n"
- + "|1.Modificas Nombre: |\n"
- + "|2.Modificar edad: |\n"
- + "|3.Modificar tienda en la que trabaja: |\n"
- + "|4.Modificar trabajo que ejerce: |\n"
- + "|5.Modificar trabajo contrato: |\n"
- + "|6.Modificar trabajo que cuota: |\n"
- + "|7.Modificar ventas: |\n"
- + "|0.Volver |\n"
- + "-----------------------------------");
- op = teclado.nextInt();
- teclado.nextLine();
- switch (op) {
- case 1:
- cambiarNombre();
- break;
- case 2:
- cambiarEdad();
- break;
- case 3:
- cambiarTienda();
- break;
- case 4:
- cambiarEjerce();
- break;
- case 5:
- cambiarContrato();
- break;
- case 6:
- cambiarCuota();
- break;
- case 7:
- cambiarVentas();
- break;
- case 0:
- System.out.println("Volviendo...\n");
- break;
- default:
- System.out.println("Opcion no valida");
- }
- } while (op != 0);
- {
- }
- }
- static void cambiarNombre() throws SQLException {
- System.out.println(" ");
- empleadosTodos();
- System.out.println("Introducir numemp para modificar: ");
- String num = teclado.nextLine();
- System.out.println("Nuevo nombre del Empleado " + num + ": ");
- String nombre = teclado.nextLine();
- sql = "Update Empleados Set "
- + "nombre = '" + nombre + "' "
- + "Where numemp = '" + num + "';";
- sentencia = con.createStatement();
- sentencia.executeUpdate(sql);
- System.out.println("Empleados modificado correctamente.");
- }
- static void cambiarEdad() throws SQLException {
- System.out.println(" ");
- empleadosTodos();
- System.out.println("Introducir numemp para modificar: ");
- String num = teclado.nextLine();
- System.out.println("Nueva Edad del Empleado " + num + ": ");
- String edad = teclado.nextLine();
- sql = "Update Empleados Set "
- + "edad = '" + edad + "' "
- + "Where numemp = '" + num + "';";
- sentencia = con.createStatement();
- sentencia.executeUpdate(sql);
- System.out.println("Empleados modificado correctamente.");
- }
- static void cambiarTienda() throws SQLException {
- System.out.println(" ");
- empleadosTodos();
- System.out.println("Introducir numemp para modificar: ");
- String num = teclado.nextLine();
- System.out.println("Nueva Tienda del Empleado " + num + ": ");
- String tienda = teclado.nextLine();
- sql = "Update Empleados Set "
- + "tienda = '" + tienda + "' "
- + "Where numemp = '" + num + "';";
- sentencia = con.createStatement();
- sentencia.executeUpdate(sql);
- System.out.println("Empleados modificado correctamente.");
- }
- static void cambiarEjerce() throws SQLException {
- System.out.println(" ");
- empleadosTodos();
- System.out.println("Introducir numemp para modificar: ");
- String num = teclado.nextLine();
- System.out.println("Introduce el nuevo trabajo que Ejerce del Empleado " + num + ": ");
- String ejerce = teclado.nextLine();
- sql = "Update Empleados Set "
- + "ejerce = '" + ejerce + "' "
- + "Where numemp = '" + num + "';";
- sentencia = con.createStatement();
- sentencia.executeUpdate(sql);
- System.out.println("Empleados modificado correctamente.");
- }
- static void cambiarContrato() throws SQLException {
- System.out.println(" ");
- empleadosTodos();
- System.out.println("Introducir numemp para modificar: ");
- String num = teclado.nextLine();
- System.out.println("Nuevo contrato del Empleado " + num + ": ");
- String contrato = teclado.nextLine();
- sql = "Update Empleados Set "
- + "contrato = '" + contrato + "' "
- + "Where numemp = '" + num + "';";
- sentencia = con.createStatement();
- sentencia.executeUpdate(sql);
- System.out.println("Empleados modificado correctamente.");
- }
- static void cambiarCuota() throws SQLException {
- System.out.println(" ");
- empleadosTodos();
- System.out.println("Introducir numemp para modificar: ");
- String num = teclado.nextLine();
- System.out.println("Nueva Cuota del Empleado " + num + ": ");
- String cuota = teclado.nextLine();
- sql = "Update Empleados Set "
- + "cuota = '" + cuota + "' "
- + "Where numemp = '" + num + "';";
- sentencia = con.createStatement();
- sentencia.executeUpdate(sql);
- System.out.println("Empleados modificado correctamente.");
- }
- static void cambiarVentas() throws SQLException {
- System.out.println(" ");
- empleadosTodos();
- System.out.println("Introducir numemp para modificar: ");
- String num = teclado.nextLine();
- System.out.println("Nuevas Ventas del Empleado " + num + ": ");
- String ventas = teclado.nextLine();
- sql = "Update Empleados Set "
- + "ventas = '" + ventas + "' "
- + "Where numemp = '" + num + "';";
- sentencia = con.createStatement();
- sentencia.executeUpdate(sql);
- System.out.println("Empleados modificado correctamente.");
- }
- static void tiendas() throws SQLException {
- Integer op;
- do {
- System.out.println("-----------------------------------\n"
- + "||Menu Principal ||\n"
- + "-----------------------------------\n"
- + "|1.Mostrar Tiendas |\n"
- + "|2.Añadir Tiendas |\n"
- + "|3.Eliminar Tiendas |\n"
- + "|4.Modificar Tiendas |\n"
- + "|0.Volver |\n"
- + "-----------------------------------");
- op = teclado.nextInt();
- teclado.nextLine();
- switch (op) {
- case 1:
- mostrarTiendas();
- break;
- // case 2:
- // añadirTiendas(sentencia, tienda);
- // break;
- // case 3:
- // eliminarTiendas(sentencia, tienda);
- // break;
- // case 4:
- // modificarTiendas(sentencia, tienda);
- case 0:
- System.out.println("Volviendo...\n");
- break;
- default:
- System.out.println("Opcion no valida");
- }
- } while (op != 0);
- }
- static void mostrarTiendas() throws SQLException {
- Integer op;
- do {
- System.out.println("-----------------------------------\n"
- + "||Menu Cosultas Tiendas ||\n"
- + "-----------------------------------\n"
- + "|1.Mostrar todas las tiendas: |\n"
- + "|2.Buscar por ciudad: |\n"
- + "|3.Buscar ventas realizada en tiendas: |\n"
- + "|0.Volver. |\n"
- + "-----------------------------------");
- op = teclado.nextInt();
- teclado.nextLine();
- switch (op) {
- case 1:
- todasTiendas();
- break;
- case 2:
- busquedaCiudad();
- break;
- case 3:
- tiendasVentas();
- break;
- case 0:
- System.out.println("Volviendo...\n");
- break;
- default:
- System.out.println("Opcion no valida");
- }
- } while (op != 0);
- }
- static void todasTiendas() throws SQLException {
- try {
- sql = "SELECT * FROM Tiendas;";
- sentencia = con.createStatement();
- rs = sentencia.executeQuery(sql);
- while (rs.next()) {
- String res = rs.getString("tienda") + "\t "
- + rs.getString("ciudad") + "\t"
- + rs.getString("dir") + "\t"
- + rs.getString("ventas") + "\t";
- System.out.println(res);
- }
- } catch (SQLException ex) {
- System.err.println("Error: " + ex);
- }
- }
- static void busquedaCiudad() throws SQLException {
- try {
- System.out.println("Se ha establecido la conexión...");
- System.out.println("Introduce la ciudad a mostrar: ");
- String ciu = teclado.nextLine();
- sql = "SELECT * FROM Tiendas where ciudad = \'" + ciu + "\';";
- sentencia = con.createStatement();
- rs = sentencia.executeQuery(sql);
- boolean ciudad = false;
- while (rs.next()) {
- String res = rs.getString("tienda") + "\t "
- + rs.getString("ciudad") + "\t"
- + rs.getString("dir") + "\t"
- + rs.getString("ventas") + "\t";
- System.out.println(res);
- ciudad = true;
- }
- if (ciudad == true) {
- System.out.println("Ciudad encontrada. Pulse para continuar!");
- String test = teclado.nextLine();
- } else {
- System.out.println("La Ciudad no a sido encontrada. Pulse para continuar!");
- }
- } catch (SQLException ex) {
- System.err.println("Error: " + ex);
- }
- }
- static void tiendasVentas() throws SQLException {
- try {
- System.out.println("Se ha establecido la conexión...");
- System.out.println("Introduce la cantidad de ventas de mayor a menor: ");
- String pre = teclado.nextLine();
- sql = "SELECT * FROM Tiendas where ventas <= " + pre + "";
- sentencia = con.createStatement();
- rs = sentencia.executeQuery(sql);
- boolean salida = false;
- while (rs.next()) {
- String res = rs.getString("tienda") + "\t "
- + rs.getString("ciudad") + "\t"
- + rs.getString("dir") + "\t"
- + rs.getString("ventas") + "\t";
- System.out.println(res);
- salida = true;
- }
- if (salida == true) {
- System.out.println("Cantidad encontrada. Pulse para continuar!");
- String test = teclado.nextLine();
- } else {
- System.out.println("La consulta no ha mostrado resultado. Pulse para continuar!");
- }
- } catch (SQLException ex) {
- System.err.println("Error: " + ex);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement