Advertisement
Guest User

fin empleados

a guest
Jan 27th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 17.49 KB | None | 0 0
  1. package proyectoyomusico;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8. import java.util.Scanner;
  9. import java.util.logging.Level;
  10. import java.util.logging.Logger;
  11.  
  12. /**
  13.  *
  14.  * @author Cristian
  15.  */
  16. public class ProyectoYomusico {
  17.  
  18.     static Scanner teclado = new Scanner(System.in);
  19.     static String sentenciaSQL;
  20.     static com.mysql.jdbc.Connection con;
  21.     static String sql = null;
  22.     static Statement sentencia;
  23.     static ResultSet rs;
  24.     static String empleado = null;
  25.     static String consulta = null;
  26.  
  27.     public static void main(String[] args) throws ClassNotFoundException, SQLException {
  28.         int menu = 0;
  29.  
  30.         try {
  31.             Class.forName("com.mysql.jdbc.Driver");
  32.  
  33.             String url = "jdbc:mysql://localhost/yomusico";
  34.             con = (com.mysql.jdbc.Connection) DriverManager.getConnection(url, "root", "1234");
  35.  
  36.             do {
  37.                 Integer op;
  38.                 System.out.println("-----------------------------------\n"
  39.                         + "||Menu Principal                 ||\n"
  40.                         + "-----------------------------------\n"
  41.                         + "|1.Empleados                      |\n"
  42.                         + "|2.Tiendas                        |\n"
  43.                         + "|3.Pedidos                        |\n"
  44.                         + "|4.Clientes                       |\n"
  45.                         + "|5.Instrumentos                   |\n"
  46.                         + "|0.Salir                          |\n"
  47.                         + "-----------------------------------");
  48.                 op = teclado.nextInt();
  49.  
  50.                 switch (op) {
  51.                     case 1:
  52.                         Empleados();
  53.                         break;
  54. //                case 2:
  55. //                    Tiendas();
  56. //                    break;
  57. //                case 3:
  58. //                    Pedidos();
  59. //                    break;
  60. //                case 4:
  61. //                    Clientes();
  62. //                    break;
  63. //                case 5:
  64. //                    Instrumentos();
  65. //                    break;
  66.                     case 0:
  67.                         System.out.println("Saliendo...\n\nHasta Pronto");
  68.                         break;
  69.                     default:
  70.                         System.out.println("Opcion no valida");
  71.                 }
  72.             } while (menu != 2);
  73.             con.close();
  74.  
  75.         } catch (ClassNotFoundException | SQLException ex) {
  76.             System.err.println("Error: " + ex);
  77.         }
  78.     }
  79.  
  80.     static void Empleados() throws SQLException {
  81.         Integer op;
  82.         do {
  83.             System.out.println("-----------------------------------\n"
  84.                     + "||Menu Principal                 ||\n"
  85.                     + "-----------------------------------\n"
  86.                     + "|1.Mostrar Empleados              |\n"
  87.                     + "|2.Añadir Empleados               |\n"
  88.                     + "|3.Eliminar Empleados             |\n"
  89.                     + "|4.Modificar Empleados            |\n"
  90.                     + "|0.Salir                          |\n"
  91.                     + "-----------------------------------");
  92.             op = teclado.nextInt();
  93.             teclado.nextLine();
  94.             if (op == null) {
  95.             }
  96.             switch (op) {
  97.                 case 1:
  98.                     mostrarEmpleados(sentencia, empleado);
  99.                     break;
  100.                 case 2:
  101.                     añadirEmpleados(sentencia, empleado);
  102.                     break;
  103.                 case 3:
  104.                     eliminarEmpleados(sentencia, empleado);
  105.                     break;
  106.                 case 4:
  107.                     modificarEmpleados(sentencia, empleado);
  108.                 case 0:
  109.                     System.out.println("Volviendo...\n");
  110.                     break;
  111.                 default:
  112.                     System.out.println("Opcion no valida");
  113.             }
  114.         } while (op != 0);
  115.     }
  116.  
  117.     static void mostrarEmpleados(Statement sentencia, String empleado) throws SQLException {
  118.         Integer op;
  119.         do {
  120.             System.out.println("-----------------------------------\n"
  121.                     + "||Menu Modificación Empleados            ||\n"
  122.                     + "-----------------------------------\n"
  123.                     + "|1.Buscar por Nombre:                     |\n"
  124.                     + "|2.Buscar por Tienda:                       |\n"
  125.                     + "|3.Busqueda por ventas:                   |\n"
  126.                     + "|0.Volver.                                |\n"
  127.                     + "-----------------------------------");
  128.             op = teclado.nextInt();
  129.             teclado.nextLine();
  130.             if (op == null) {
  131.             }
  132.             switch (op) {
  133.                 case 1:
  134.                     EmpleadosTodos(sentencia, empleado);
  135.                     break;
  136.                 case 2:
  137.                     EmpleadosTiendas(sentencia, empleado);
  138.                     break;
  139.                 case 3:
  140.                     EmpleadosVentas(sentencia, empleado);
  141.                     break;
  142.                 case 0:
  143.                     System.out.println("Volviendo...\n");
  144.                     break;
  145.                 default:
  146.                     System.out.println("Opcion no valida");
  147.             }
  148.         } while (op != 0);
  149.     }
  150.  
  151.     static void EmpleadosTodos(Statement sentencia, String empleado) throws SQLException {
  152.         try {
  153.             System.out.println("Se ha establecido la conexión...");
  154.  
  155.             sql = "SELECT * FROM Empleados;";
  156.             sentencia = con.createStatement();
  157.             rs = sentencia.executeQuery(sql);
  158.             while (rs.next()) {
  159.                 String res = rs.getString("numemp") + "\t "
  160.                         + rs.getString("nombre") + "\t"
  161.                         + rs.getString("edad") + "\t"
  162.                         + rs.getString("tienda") + "\t"
  163.                         + rs.getString("ejerce") + "\t"
  164.                         + rs.getString("contrato") + "\t"
  165.                         + rs.getString("cuota") + "\t"
  166.                         + rs.getString("ventas");
  167.                 System.out.println(res);
  168.             }
  169.         } catch (SQLException ex) {
  170.             System.err.println("Error: " + ex);
  171.         }
  172.     }
  173.  
  174.     static void EmpleadosTiendas(Statement sentencia, String empleado) throws SQLException {
  175.         try {
  176.             System.out.println("Se ha establecido la conexión...");
  177.  
  178.             sql = "SELECT * FROM Empleados where order by tienda;";
  179.             sentencia = con.createStatement();
  180.             rs = sentencia.executeQuery(sql);
  181.             while (rs.next()) {
  182.                 String res = rs.getString("numemp") + "\t "
  183.                         + rs.getString("nombre") + "\t"
  184.                         + rs.getString("edad") + "\t"
  185.                         + rs.getString("tienda") + "\t"
  186.                         + rs.getString("ejerce") + "\t"
  187.                         + rs.getString("contrato") + "\t"
  188.                         + rs.getString("cuota") + "\t"
  189.                         + rs.getString("ventas");
  190.                 System.out.println(res);
  191.             }
  192.         } catch (SQLException ex) {
  193.             System.err.println("Error: " + ex);
  194.         }
  195.     }
  196.  
  197.     static void EmpleadosVentas(Statement sentencia, String empleado) throws SQLException {
  198.         try {
  199.             System.out.println("Se ha establecido la conexión...");
  200.  
  201.             sql = "SELECT * FROM Empleados where order by tienda;";
  202.             sentencia = con.createStatement();
  203.             rs = sentencia.executeQuery(sql);
  204.             while (rs.next()) {
  205.                 String res = rs.getString("numemp") + "\t "
  206.                         + rs.getString("nombre") + "\t"
  207.                         + rs.getString("edad") + "\t"
  208.                         + rs.getString("tienda") + "\t"
  209.                         + rs.getString("ejerce") + "\t"
  210.                         + rs.getString("contrato") + "\t"
  211.                         + rs.getString("cuota") + "\t"
  212.                         + rs.getString("ventas");
  213.                 System.out.println(res);
  214.             }
  215.         } catch (SQLException ex) {
  216.             System.err.println("Error: " + ex);
  217.         }
  218.     }
  219.  
  220.     static boolean existeEmpleados(Statement sentencia, String empleado) {
  221.         boolean existe = false;
  222.         String s = "SELECT * FROM Empleado WHERE nombre=\'" + empleado + "\'";
  223.         ResultSet rs;
  224.         try {
  225.             rs = sentencia.executeQuery(s);
  226.             if (rs.next()) {
  227.                 existe = true;
  228.             } else {
  229.                 existe = false;
  230.             }
  231.         } catch (SQLException ex) {
  232.  
  233.         }
  234.         return existe;
  235.     }
  236.  
  237.     static void añadirEmpleados(Statement sentencia, String empleado) throws SQLException {
  238.         System.out.println("Introduce el numemp (número id): ");
  239.         String num = teclado.nextLine();
  240.         System.out.println("Introduce el Nombre del empleado: ");
  241.         String nom = teclado.nextLine();
  242.         System.out.println("Introduce la edad del empleado: ");
  243.         String edad = teclado.nextLine();
  244.         System.out.println("Introduce la tienda donde trabaja el Empleado a introducir(numero de la tienda.): ");
  245.         String tien = teclado.nextLine();
  246.         System.out.println("Introduce el trabajo que realiza el empleado: ");
  247.         String ejer = teclado.nextLine();
  248.         System.out.println("Introduce la fecha del contrato de cuando se realizó(AÑO-MES-DIA): ");
  249.         String contrato = teclado.nextLine();
  250.         System.out.println("Introduce la cuota de dicho Empleado: ");
  251.         String cuota = teclado.nextLine();
  252.         System.out.println("Introduce las ventas realizada por el Empleado: ");
  253.         String ventas = teclado.nextLine();
  254.         empleado = "INSERT INTO Empleados VALUE ('" + num + "','" + nom + "','" + edad + "','" + tien + "','" + ejer + "','" + contrato + "' ,'" + cuota + "','" + ventas + "');";
  255.         sentencia = con.createStatement();
  256.         sentencia.executeUpdate(empleado);
  257.         System.out.println("Empleado registrado");
  258.         String test = teclado.nextLine();
  259.     }
  260.  
  261.     static void eliminarEmpleados(Statement sentencia, String empleado) throws SQLException {
  262.         // BORRA UN EMPLEADO
  263.         mostrarEmpleados(sentencia, empleado);
  264.         System.out.println("Introducir el numemp del Empleado para borrar: ");
  265.         String numemp = teclado.nextLine();
  266.         String sql = "Delete From Empleados Where numemp = '" + numemp + "';";
  267.         sentencia = con.createStatement();
  268.         sentencia.executeUpdate(sql);
  269.         System.out.println("Empleado Borrado! Pulse para continuar!");
  270.         String test = teclado.nextLine();
  271.     }
  272.  
  273.     static void modificarEmpleados(Statement sentencia, String empleado) throws SQLException {
  274. //        // MODIFICA UN Empleado
  275.         Integer op;
  276.         do {
  277.             System.out.println("-----------------------------------\n"
  278.                     + "||Menu Modificación Empleados            ||\n"
  279.                     + "-----------------------------------\n"
  280.                     + "|1.Modificas Nombre:                     |\n"
  281.                     + "|2.Modificar edad:                       |\n"
  282.                     + "|3.Modificar tienda en la que trabaja:   |\n"
  283.                     + "|4.Modificar trabajo que ejerce:         |\n"
  284.                     + "|5.Modificar trabajo contrato:           |\n"
  285.                     + "|6.Modificar trabajo que cuota:          |\n"
  286.                     + "|7.Modificar ventas:                     |\n"
  287.                     + "|0.Salir                                 |\n"
  288.                     + "-----------------------------------");
  289.             op = teclado.nextInt();
  290.             teclado.nextLine();
  291.             if (op == null) {
  292.             }
  293.             switch (op) {
  294.                 case 1:
  295.                     cambiarNombre(sentencia, empleado);
  296.                     break;
  297.                 case 2:
  298.                     cambiarEdad(sentencia, empleado);
  299.                     break;
  300.                 case 3:
  301.                     cambiarTienda(sentencia, empleado);
  302.                     break;
  303.                 case 4:
  304.                     cambiarEjerce(sentencia, empleado);
  305.                     break;
  306.                 case 5:
  307.                     cambiarContrato(sentencia, empleado);
  308.                     break;
  309.                 case 6:
  310.                     cambiarCuota(sentencia, empleado);
  311.                     break;
  312.                 case 7:
  313.                     cambiarVentas(sentencia, empleado);
  314.                     break;
  315.                 case 0:
  316.                     System.out.println("Volviendo...\n");
  317.                     break;
  318.                 default:
  319.                     System.out.println("Opcion no valida");
  320.             }
  321.         } while (op != 0);
  322.         {
  323.         }
  324.     }
  325.  
  326.     static void cambiarNombre(Statement sentencia, String empleado) throws SQLException {
  327.         System.out.println("    ");
  328.         mostrarEmpleados(sentencia, empleado);
  329.         System.out.println("Introducir numemp para modificar: ");
  330.         String num = teclado.nextLine();
  331.         System.out.println("Nuevo nombre del Empleado " + num + ": ");
  332.         String nombre = teclado.nextLine();
  333.         empleado = "Update Empleados Set "
  334.                 + "nombre = '" + nombre + "' ";
  335.         sentencia = con.createStatement();
  336.         sentencia.executeUpdate(empleado);
  337.         System.out.println("Empleados modificado correctamente.");
  338.     }
  339.  
  340.     static void cambiarEdad(Statement sentencia, String empleado) throws SQLException {
  341.         System.out.println("    ");
  342.         mostrarEmpleados(sentencia, empleado);
  343.         System.out.println("Introducir numemp para modificar: ");
  344.         String num = teclado.nextLine();
  345.         System.out.println("Nueva Edad del Empleado " + num + ": ");
  346.         String edad = teclado.nextLine();
  347.         empleado = "Update Empleados Set "
  348.                 + "edad = '" + edad + "' ";
  349.         sentencia = con.createStatement();
  350.         sentencia.executeUpdate(empleado);
  351.         System.out.println("Empleados modificado correctamente.");
  352.     }
  353.  
  354.     static void cambiarTienda(Statement sentencia, String empleado) throws SQLException {
  355.         System.out.println("    ");
  356.         mostrarEmpleados(sentencia, empleado);
  357.         System.out.println("Introducir numemp para modificar: ");
  358.         String num = teclado.nextLine();
  359.         System.out.println("Nueva Tienda del Empleado " + num + ": ");
  360.         String tienda = teclado.nextLine();
  361.         empleado = "Update Empleados Set "
  362.                 + "tienda = '" + tienda + "' ";
  363.         sentencia = con.createStatement();
  364.         sentencia.executeUpdate(empleado);
  365.         System.out.println("Empleados modificado correctamente.");
  366.     }
  367.  
  368.     static void cambiarEjerce(Statement sentencia, String empleado) throws SQLException {
  369.         System.out.println("    ");
  370.         mostrarEmpleados(sentencia, empleado);
  371.         System.out.println("Introducir numemp para modificar: ");
  372.         String num = teclado.nextLine();
  373.         System.out.println("Introduce el nuevo trabajo que Ejerce del Empleado " + num + ": ");
  374.         String ejerce = teclado.nextLine();
  375.         empleado = "Update Empleados Set "
  376.                 + "ejerce = '" + ejerce + "' ";
  377.         sentencia = con.createStatement();
  378.         sentencia.executeUpdate(empleado);
  379.         System.out.println("Empleados modificado correctamente.");
  380.     }
  381.  
  382.     static void cambiarContrato(Statement sentencia, String empleado) throws SQLException {
  383.         System.out.println("    ");
  384.         mostrarEmpleados(sentencia, empleado);
  385.         System.out.println("Introducir numemp para modificar: ");
  386.         String num = teclado.nextLine();
  387.         System.out.println("Nuevo contrato del Empleado " + num + ": ");
  388.         String contrato = teclado.nextLine();
  389.         empleado = "Update Empleados Set "
  390.                 + "contrato = '" + contrato + "' ";
  391.         sentencia = con.createStatement();
  392.         sentencia.executeUpdate(empleado);
  393.         System.out.println("Empleados modificado correctamente.");
  394.     }
  395.  
  396.     static void cambiarCuota(Statement sentencia, String empleado) throws SQLException {
  397.         System.out.println("    ");
  398.         mostrarEmpleados(sentencia, empleado);
  399.         System.out.println("Introducir numemp para modificar: ");
  400.         String num = teclado.nextLine();
  401.         System.out.println("Nueva Cuota del Empleado " + num + ": ");
  402.         String cuota = teclado.nextLine();
  403.         empleado = "Update Empleados Set "
  404.                 + "cuota = '" + cuota + "' ";
  405.         sentencia = con.createStatement();
  406.         sentencia.executeUpdate(empleado);
  407.         System.out.println("Empleados modificado correctamente.");
  408.     }
  409.  
  410.     static void cambiarVentas(Statement sentencia, String empleado) throws SQLException {
  411.         System.out.println("    ");
  412.         mostrarEmpleados(sentencia, empleado);
  413.         System.out.println("Introducir numemp para modificar: ");
  414.         String num = teclado.nextLine();
  415.         System.out.println("Nuevas Ventas del Empleado " + num + ": ");
  416.         String ventas = teclado.nextLine();
  417.         empleado = "Update Empleados Set "
  418.                 + "ventas = '" + ventas + "' ";
  419.         sentencia = con.createStatement();
  420.         sentencia.executeUpdate(empleado);
  421.         System.out.println("Empleados modificado correctamente.");
  422.     }
  423. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement