Advertisement
Guest User

JDBC Empleados

a guest
Jan 26th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.36 KB | None | 0 0
  1. package yomusico;
  2.  
  3. import java.sql.DriverManager;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7. import java.util.Scanner;
  8.  
  9. /**
  10.  *
  11.  * @author cristian
  12.  */
  13. public class Yomusico {
  14.  
  15.     static Scanner teclado = new Scanner(System.in);
  16.     // CONEXIÓN A LA BASE DE DATOS
  17.     static String servidor = "jdbc:mysql://localhost/yomusico";
  18.     static String usuario = "root";
  19.     static String contraseña = "1234";
  20.     private static String num;
  21.  
  22.     private static void menu() {
  23.         Integer op;
  24.         do {
  25.             System.out.println("-----------------------------------\n"
  26.                     + "||Menu Principal                 ||\n"
  27.                     + "-----------------------------------\n"
  28.                     + "|1.Empleados                        |\n"
  29.                     + "|2.Tiendas                        |\n"
  30.                     + "|3.Pedidos                       |\n"
  31.                     + "|4.Clientes                       |\n"
  32.                     + "|5.Instrumentos                  |\n"
  33.                     + "|0.Salir                          |\n"
  34.                     + "-----------------------------------");
  35.             op = teclado.nextInt();
  36.             if (op == null) {
  37.                 op = 100;
  38.             }
  39.             switch (op) {
  40.                 case 1:
  41.                     Empleados();
  42.                     break;
  43.                 case 2:
  44.                     Tiendas();
  45.                     break;
  46.                 case 3:
  47.                     Pedidos();
  48.                     break;
  49.                 case 4:
  50.                     Clientes();
  51.                     break;
  52.                 case 5:
  53.                     Instrumentos();
  54.                     break;
  55.                 case 0:
  56.                     System.out.println("Saliendo...\n\nHasta Pronto");
  57.                     break;
  58.                 default:
  59.                     System.out.println("Opcion no valida");
  60.             }
  61.         } while (op != 0);
  62.     }
  63. //    TABLA EMPLEADOS
  64.  
  65.     private static void Empleados() {
  66.         Integer op;
  67.         do {
  68.             System.out.println("-----------------------------------\n"
  69.                     + "||Menu Principal                 ||\n"
  70.                     + "-----------------------------------\n"
  71.                     + "|1.Mostrar Empleados                        |\n"
  72.                     + "|2.Tiendas                        |\n"
  73.                     + "|3.Pedidos                       |\n"
  74.                     + "|4.Clientes                       |\n"
  75.                     + "|5.Instrumentos                  |\n"
  76.                     + "|0.Salir                          |\n"
  77.                     + "-----------------------------------");
  78.             op = teclado.nextInt();
  79.             if (op == null) {
  80.                 op = 100;
  81.             }
  82.             switch (op) {
  83.                 case 1:
  84.                     mostrarEmpleados();
  85.                     break;
  86.                 case 2:
  87.                     añadirEmpleados();
  88.                     break;
  89.                 case 3:
  90.                     eliminarEmpleados();
  91.                     break;
  92.                 case 4:
  93.                     modificarEmpleados();
  94.                 case 0:
  95.                     System.out.println("Volviendo...\n");
  96.                     break;
  97.                 default:
  98.                     System.out.println("Opcion no valida");
  99.             }
  100.         } while (op != 0);
  101.     }
  102.  
  103.     private static void mostrarEmpleados(Statement sentencia, String empleado) {
  104.         try {
  105.             Scanner sc = new Scanner(System.in);
  106.             System.out.println("Introduce el nombre del Empleado a buscar:");
  107.             empleado = sc.nextLine();
  108.             String sentenciaSQL = "SELECT * FROM Empleados where nombre =\'" + empleado + "\'";
  109.             ResultSet rs = sentencia.executeQuery(sentenciaSQL);
  110.  
  111.             while (rs.next()) {
  112.                 String res = rs.getString("numemp") + "\t "
  113.                         + rs.getString("nombre") + "\t"
  114.                         + rs.getString("edad") + "\t"
  115.                         + rs.getString("oficina") + "\t"
  116.                         + rs.getString("titulo") + "\t"
  117.                         + rs.getString("contrato") + "\t"
  118.                         + rs.getString("jefe") + "\t"
  119.                         + rs.getString("cuota") + "\t"
  120.                         + rs.getString("ventas");
  121.                 System.out.println(res);
  122.             }
  123.  
  124.             if (!existeEmpleado(sentencia, empleado)) {
  125.                 System.out.println("No existe este Empleado.");
  126.             } else {
  127.                 mostrarEmpleados(sentencia, empleado);
  128.             }
  129.  
  130.         } catch (SQLException ex) {
  131.             System.out.println("Error SQL");
  132.         }
  133.     }
  134.  
  135.     static boolean existeEmpleado(Statement sentencia, String empleado) {
  136.         boolean existe = false;
  137.         String s = "SELECT * FROM Empleado WHERE nombre=\'" + empleado + "\'";
  138.         ResultSet rs;
  139.         try {
  140.             rs = sentencia.executeQuery(s);
  141.             if (rs.next()) {
  142.                 existe = true;
  143.             } else {
  144.                 existe = false;
  145.             }
  146.         } catch (SQLException ex) {
  147.  
  148.         }
  149.         return existe;
  150.     }
  151.  
  152.     private static void añadirEmpleados() {
  153.         System.out.println("Registrar Empleado");
  154.         String consulta;
  155.         System.out.println("Introduce la referencia");
  156.         int ref = teclado.nextInt();
  157.  
  158.         //comprobar si el suelo existe
  159.         if (!repetido("Suelos", "ref", ref)) {
  160.             System.out.println("Introduce el numemp (número id): ");
  161.             int num = teclado.nextInt();
  162.             System.out.println("Introduce el Nombre del empleado: ");
  163.             String nom = teclado.nextLine();
  164.             System.out.println("Introduce la edad del empleado: ");
  165.             int edad = teclado.nextInt();
  166.             System.out.println("Introduce la tienda donde trabaja el Empleado a introducir: ");
  167.             String tien = teclado.nextLine();
  168.             System.out.println("Introduce el trabajo que realiza el empleado: ");
  169.             String ejer = teclado.nextLine();
  170.             System.out.println("Introduce la fecha del contrato de cuando se realizó: ");
  171.             int contrato = teclado.nextInt();
  172.             System.out.println("Introduce la cuota de dicho Empleado: ");
  173.             String cuota = teclado.nextLine();
  174.             System.out.println("Introduce las ventas realizada por el Empleado: ");
  175.             String ventas = teclado.nextLine();
  176.             consulta = "INSERT INTO Empleados VALUE ('" + num + "','" + nom + "','" + edad + "','" + tien + "','" + ejer + "','" + contrato + "' ,'" + cuota + "','" + ventas + "');";
  177.             System.out.println("Suelo registrado");
  178.         } else {
  179.             System.out.println("El Empleado ya existe.");
  180.         }
  181.  
  182.     }
  183.  
  184.     private static void eliminarEmpleados() {
  185.         String consulta;
  186.         System.out.println("Introduzca el numemp del Empleado que deseas eliminar: ");
  187.         String numemp = teclado.nextLine();
  188.         if (existe("Empleados", "numemp", numemp)) {
  189.             System.out.println("Los Empleados con la referencia " + numemp + " son:");
  190.             sueloCampo("numep", numemp);
  191.             System.out.println("Desea continuar? (s/n)");
  192.             char sn = teclado.nextLine();
  193.             if (sn == 's') {
  194.                 consulta = "DELETE FROM Empleados WHERE numemp = '" + numemp + "';";
  195.                 System.out.println("Empleado eliminado con exito.");
  196.             } else {
  197.                 System.out.println("Volviendo...\n");
  198.             }
  199.         } else {
  200.             System.out.println("No existen Empleados con este " + numemp);
  201.         }
  202.     }
  203.  
  204.     private static boolean repetido(String suelos, String ref, int ref0) {
  205.         throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  206.     }
  207.  
  208.     private static boolean existe(String suelos, String ref, String ref0) {
  209.         throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  210.     }
  211.  
  212.     private static void sueloCampo(String numep, String numemp) {
  213.         throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  214.     }
  215.     private static void modificarEmpleado() {
  216.         Integer op;
  217.         System.out.println("Introduzca la referencia");
  218.         String iden = teclado.nextLine();
  219.         if (existe("Suelos", "ref", iden)) {
  220.             System.out.println("Los suelos con referencia: " + iden + " son:");
  221.             sueloCampo("ref", iden);
  222.             System.out.println("\nSe modificaran todos los suelos mostrados\nDesea continuar? (s/n)");
  223.             String sn = teclado.nextLine();
  224.             if (sn == 's') {
  225.                 do {
  226.                     System.out.println("-----------------------------------\n"
  227.                             + "||Modifición de Empleados               ||\n"
  228.                             + "-----------------------------------\n"
  229.                             + "|1.Modificar Nombre                |\n"
  230.                             + "|2.Modificar Edad               |\n"
  231.                             + "|3.Modificar Tienda                 |\n"
  232.                             + "|4.Modificar Trabajo que ejerce                |\n"
  233.                             + "|5.Modificar Contrato            |\n"
  234.                             + "|6.Modificar Cuota            |\n"
  235.                             + "|7.Modificar Ventas            |\n"
  236.                             + "|0.Volver                         |\n"
  237.                             + "-----------------------------------");
  238.                     op = teclado.nextInt();
  239.                     if (op == null) {
  240.                         op = 100;
  241.                     }                    switch (op) {
  242.                         case 1:
  243.                             //Nombre
  244.                             System.out.println("Introduzca el nuevo Nombre: ");
  245.                             String nombre = teclado.nextLine();
  246.  
  247.                             break;
  248.                         case 2:
  249.                             //Edad
  250.                             System.out.println("Introduzca la nueva edad: ");
  251.                             int edad = teclado.nextInt();
  252.                             break;
  253.                         case 3:
  254.                             //tienda
  255.                             System.out.println("Introduzca la nueva tienda: ");
  256.                             int tienda = teclado.nextInt();
  257.                            
  258.                             break;
  259.                         case 4:
  260.                             //EJERCE
  261.                             System.out.println("Introduzca el nuevo trabajo: ");
  262.                             String ejerce = teclado.nextLine();
  263.                            
  264.                             break;
  265.                         case 5:
  266.                             //CONTRATO
  267.                             System.out.println("Introduzca el nuevo contrato: ");
  268.                             int contrato = teclado.nextInt();
  269.                             break;
  270.                         case 6:
  271.                             //CUOTA
  272.                             System.out.println("Introduzca la nueva cuota: ");
  273.                             int cuota = teclado.nextInt();
  274.                             break;
  275.                         case 7:
  276.                             //VENTAS
  277.                             System.out.println("Introduzca las nuevas ventas: ");
  278.                             int ventas = teclado.nextInt();
  279.                             break;
  280.                         case 0:
  281.                             System.out.println("Volviendo...\n");
  282.                             break;
  283.                         default:
  284.                             System.out.println("Opcion no valida");
  285.                     }
  286.                     if (op != 100 && op != 0) {
  287.                         System.out.println("Pulse 'Intro' para continuar");
  288.                         teclado.nextLine();
  289.                     }
  290.                 } while (op != 0);
  291.             } else {
  292.                 System.out.println("Volviendo...\n");
  293.             }
  294.         } else {
  295.             System.out.println("No exiten Empleados con la referencia " + iden);
  296.         }
  297.     }
  298. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement