Advertisement
Guest User

JDBC

a guest
Jan 29th, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 23.72 KB | None | 0 0
  1. package proyectoyomusico;
  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 ProyectoYomusico {
  14.  
  15.     static Scanner teclado = new Scanner(System.in, "UTF-8");
  16.     static com.mysql.jdbc.Connection con;
  17.     static String sql;
  18.     static Statement sentencia;
  19.     static ResultSet rs;
  20.  
  21.     public static void main(String[] args) throws ClassNotFoundException, SQLException {
  22.         int menu = 0;
  23.  
  24.         try {
  25.             Class.forName("com.mysql.jdbc.Driver");
  26.  
  27.             String url = "jdbc:mysql://localhost/Yomusico";
  28.             con = (com.mysql.jdbc.Connection) DriverManager.getConnection(url, "root", "");
  29.  
  30.             do {
  31.                 Integer op;
  32.                 System.out.println("-----------------------------------\n"
  33.                         + "||Menu Principal                 ||\n"
  34.                         + "-----------------------------------\n"
  35.                         + "|1.Empleados                      |\n"
  36.                         + "|2.Tiendas                        |\n"
  37.                         + "|3.Pedidos                        |\n"
  38.                         + "|4.Clientes                       |\n"
  39.                         + "|5.Instrumentos                   |\n"
  40.                         + "|0.Salir                          |\n"
  41.                         + "-----------------------------------");
  42.                 op = teclado.nextInt();
  43.  
  44.                 switch (op) {
  45.                     case 1:
  46.                         empleados();
  47.                         break;
  48.                     case 2:
  49.                         tiendas();
  50.                         break;
  51. //                case 3:
  52. //                    Pedidos();
  53. //                    break;
  54. //                case 4:
  55. //                    Clientes();
  56. //                    break;
  57. //                case 5:
  58. //                    Instrumentos();
  59. //                    break;
  60.                     case 0:
  61.                         System.out.println("Saliendo...\n\nHasta Pronto");
  62.                         break;
  63.                     default:
  64.                         System.out.println("Opcion no valida");
  65.                 }
  66.             } while (menu != 2);
  67.             con.close();
  68.  
  69.         } catch (ClassNotFoundException | SQLException ex) {
  70.             System.err.println("Error: " + ex);
  71.         }
  72.     }
  73.  
  74.     static void empleados() throws SQLException {
  75.         Integer op;
  76.         do {
  77.             System.out.println("-----------------------------------\n"
  78.                     + "||Menu Principal                 ||\n"
  79.                     + "-----------------------------------\n"
  80.                     + "|1.Mostrar Empleados              |\n"
  81.                     + "|2.Añadir Empleados               |\n"
  82.                     + "|3.Eliminar Empleados             |\n"
  83.                     + "|4.Modificar Empleados            |\n"
  84.                     + "|0.Volver                         |\n"
  85.                     + "-----------------------------------");
  86.             op = teclado.nextInt();
  87.             teclado.nextLine();
  88.             switch (op) {
  89.                 case 1:
  90.                     mostrarEmpleados();
  91.                     break;
  92.                 case 2:
  93.                     añadirEmpleados();
  94.                     break;
  95.                 case 3:
  96.                     eliminarEmpleados();
  97.                     break;
  98.                 case 4:
  99.                     modificarEmpleados();
  100.                 case 0:
  101.                     System.out.println("Volviendo...\n");
  102.                     break;
  103.                 default:
  104.                     System.out.println("Opcion no valida");
  105.             }
  106.         } while (op != 0);
  107.     }
  108.  
  109.     static void mostrarEmpleados() throws SQLException {
  110.         Integer op;
  111.         do {
  112.             System.out.println("-----------------------------------\n"
  113.                     + "||Menu Modificación Empleados            ||\n"
  114.                     + "-----------------------------------\n"
  115.                     + "|1.Mostrar Empleados:                     |\n"
  116.                     + "|2.Buscar por Tienda:                     |\n"
  117.                     + "|3.Busqueda por ventas de cantidad :      |\n"
  118.                     + "|0.Volver.                                |\n"
  119.                     + "-----------------------------------");
  120.             op = teclado.nextInt();
  121.             teclado.nextLine();
  122.             switch (op) {
  123.                 case 1:
  124.                     empleadosTodos();
  125.                     break;
  126.                 case 2:
  127.                     empleadosTiendas();
  128.                     break;
  129.                 case 3:
  130.                     empleadosVentas();
  131.                     break;
  132.                 case 0:
  133.                     System.out.println("Volviendo...\n");
  134.                     break;
  135.                 default:
  136.                     System.out.println("Opcion no valida");
  137.             }
  138.         } while (op != 0);
  139.     }
  140.  
  141.     static void empleadosTodos() throws SQLException {
  142.         try {
  143.             System.out.println("Se ha establecido la conexión...");
  144.  
  145.             sql = "SELECT * FROM Empleados;";
  146.             sentencia = sentencia = con.createStatement();
  147.             rs = sentencia.executeQuery(sql);
  148.             boolean todos = false;
  149.             while (rs.next()) {
  150.                 String res = rs.getString("numemp") + "\t "
  151.                         + rs.getString("nombre") + "\t"
  152.                         + rs.getString("edad") + "\t"
  153.                         + rs.getString("tienda") + "\t"
  154.                         + rs.getString("ejerce") + "\t"
  155.                         + rs.getString("contrato") + "\t"
  156.                         + rs.getString("cuota") + "\t"
  157.                         + rs.getString("ventas");
  158.                 System.out.println(res);
  159.                 todos = true;
  160.             }
  161.             if (todos == true) {
  162.                 System.out.println("Empleados mostrados. Pulse para continuar!");
  163.                 String test = teclado.nextLine();
  164.             } else {
  165.                 System.out.println("Los Empleados no han sido mostrado resultados. Pulse para continuar!");
  166.  
  167.             }
  168.         } catch (SQLException ex) {
  169.             System.err.println("Error: " + ex);
  170.         }
  171.     }
  172.  
  173.     static void empleadosTiendas() throws SQLException {
  174.         try {
  175.             System.out.println("Se ha establecido la conexión...");
  176.             todasTiendas();
  177.  
  178.             System.out.println("Introduce la tienda a buscar(por numero): ");
  179.             String codtienda = teclado.nextLine();
  180.  
  181.             sql = "SELECT * FROM Empleados where tienda= " + codtienda + ";";
  182.             sentencia = sentencia = con.createStatement();
  183.             rs = sentencia.executeQuery(sql);
  184.             boolean tienda = false;
  185.             while (rs.next()) {
  186.                 String res = rs.getString("numemp") + "\t "
  187.                         + rs.getString("nombre") + "\t"
  188.                         + rs.getString("edad") + "\t"
  189.                         + rs.getString("tienda") + "\t"
  190.                         + rs.getString("ejerce") + "\t"
  191.                         + rs.getString("contrato") + "\t"
  192.                         + rs.getString("cuota") + "\t"
  193.                         + rs.getString("ventas");
  194.                 System.out.println(res);
  195.                 tienda = true;
  196.  
  197.             }
  198.  
  199.             if (tienda == true) {
  200.                 System.out.println("Tienda encontrada. Pulse para continuar!");
  201.                 String test = teclado.nextLine();
  202.             } else {
  203.                 System.out.println("La sobre Tienda no a mostrado resultados. Pulse para continuar!");
  204.  
  205.             }
  206.         } catch (SQLException ex) {
  207.             System.err.println("Error: " + ex);
  208.         }
  209.     }
  210.  
  211.     static void empleadosVentas() throws SQLException {
  212.         try {
  213.             System.out.println("Se ha establecido la conexión...");
  214.             System.out.println("Introduce la cantidad de ventas de mayor a menor: ");
  215.             String pre = teclado.nextLine();
  216.             sql = "SELECT * FROM Empleados where ventas <= " + pre + "";
  217.             sentencia = con.createStatement();
  218.             rs = sentencia.executeQuery(sql);
  219.             boolean salida = false;
  220.             while (rs.next()) {
  221.                 String res = rs.getString("numemp") + "\t "
  222.                         + rs.getString("nombre") + "\t"
  223.                         + rs.getString("edad") + "\t"
  224.                         + rs.getString("tienda") + "\t"
  225.                         + rs.getString("ejerce") + "\t"
  226.                         + rs.getString("contrato") + "\t"
  227.                         + rs.getString("cuota") + "\t"
  228.                         + rs.getString("ventas");
  229.                 System.out.println(res);
  230.                 salida = true;
  231.             }
  232.             if (salida == true) {
  233.                 System.out.println("Cantidad encontrada. Pulse para continuar!");
  234.                 String test = teclado.nextLine();
  235.             } else {
  236.                 System.out.println("La consulta no ha mostrado resultado. Pulse para continuar!");
  237.  
  238.             }
  239.         } catch (SQLException ex) {
  240.             System.err.println("Error: " + ex);
  241.         }
  242.     }
  243.  
  244.     static boolean existeEmpleados(int num) {
  245.         boolean existe = false;
  246.         sql = "SELECT * FROM Empleados WHERE numemp=" + num + ";";
  247.         try {
  248.             sentencia = con.createStatement();
  249.             rs = sentencia.executeQuery(sql);
  250.             if (rs.next()) {
  251.                 existe = true;
  252.             } else {
  253.                 existe = false;
  254.             }
  255.         } catch (SQLException ex) {
  256.  
  257.         }
  258.         return existe;
  259.     }
  260.  
  261.     static void añadirEmpleados() throws SQLException {
  262.         System.out.println("Introduce el numemp (número id): ");
  263.         int num = teclado.nextInt();
  264.         if (!existeEmpleados(num)) {
  265.             System.out.println("Introduce el Nombre del empleado: ");
  266.             String nom = teclado.nextLine();
  267.             System.out.println("Introduce la edad del empleado: ");
  268.             String edad = teclado.nextLine();
  269.             System.out.println("Introduce la tienda donde trabaja el Empleado a introducir(numero de la tienda.): ");
  270.             String tien = teclado.nextLine();
  271.             System.out.println("Introduce el trabajo que realiza el empleado: ");
  272.             String ejer = teclado.nextLine();
  273.             System.out.println("Introduce la fecha del contrato de cuando se realizó(AÑO-MES-DIA): ");
  274.             String contrato = teclado.nextLine();
  275.             System.out.println("Introduce la cuota de dicho Empleado: ");
  276.             String cuota = teclado.nextLine();
  277.             System.out.println("Introduce las ventas realizada por el Empleado: ");
  278.             String ventas = teclado.nextLine();
  279.             sql = "INSERT INTO Empleados VALUE ('" + num + "','" + nom + "','" + edad + "','" + tien + "','" + ejer + "','" + contrato + "' ,'" + cuota + "','" + ventas + "');";
  280.             sentencia = con.createStatement();
  281.             sentencia.executeUpdate(sql);
  282.         } else {
  283.             System.out.println("El numemp" + num + "Ya existe. Pruebe a introducir otro.");
  284.         }
  285.  
  286.     }
  287.  
  288.     static void eliminarEmpleados() throws SQLException {
  289.         // BORRA UN EMPLEADO
  290.         empleadosTodos();
  291.         System.out.println("Introducir el numemp del Empleado para borrar: ");
  292.         String numemp = teclado.nextLine();
  293.         sql = "Delete From Empleados Where numemp = '" + numemp + "';";
  294.         sentencia = con.createStatement();
  295.         sentencia.executeUpdate(sql);
  296.         System.out.println("Empleado Borrado! Pulse para continuar!");
  297.         String test = teclado.nextLine();
  298.     }
  299.  
  300.     static void modificarEmpleados() throws SQLException {
  301. //        // MODIFICA UN Empleado
  302.         Integer op;
  303.         do {
  304.             System.out.println("-----------------------------------\n"
  305.                     + "||Menu Modificación Empleados            ||\n"
  306.                     + "-----------------------------------\n"
  307.                     + "|1.Modificas Nombre:                     |\n"
  308.                     + "|2.Modificar edad:                       |\n"
  309.                     + "|3.Modificar tienda en la que trabaja:   |\n"
  310.                     + "|4.Modificar trabajo que ejerce:         |\n"
  311.                     + "|5.Modificar trabajo contrato:           |\n"
  312.                     + "|6.Modificar trabajo que cuota:          |\n"
  313.                     + "|7.Modificar ventas:                     |\n"
  314.                     + "|0.Volver                                 |\n"
  315.                     + "-----------------------------------");
  316.             op = teclado.nextInt();
  317.             teclado.nextLine();
  318.             switch (op) {
  319.                 case 1:
  320.                     cambiarNombre();
  321.                     break;
  322.                 case 2:
  323.                     cambiarEdad();
  324.                     break;
  325.                 case 3:
  326.                     cambiarTienda();
  327.                     break;
  328.                 case 4:
  329.                     cambiarEjerce();
  330.                     break;
  331.                 case 5:
  332.                     cambiarContrato();
  333.                     break;
  334.                 case 6:
  335.                     cambiarCuota();
  336.                     break;
  337.                 case 7:
  338.                     cambiarVentas();
  339.                     break;
  340.                 case 0:
  341.                     System.out.println("Volviendo...\n");
  342.                     break;
  343.                 default:
  344.                     System.out.println("Opcion no valida");
  345.             }
  346.         } while (op != 0);
  347.         {
  348.         }
  349.     }
  350.  
  351.     static void cambiarNombre() throws SQLException {
  352.         System.out.println("    ");
  353.         empleadosTodos();
  354.         System.out.println("Introducir numemp para modificar: ");
  355.         String num = teclado.nextLine();
  356.         System.out.println("Nuevo nombre del Empleado " + num + ": ");
  357.         String nombre = teclado.nextLine();
  358.         sql = "Update Empleados Set "
  359.                 + "nombre = '" + nombre + "' "
  360.                 + "Where numemp = '" + num + "';";
  361.         sentencia = con.createStatement();
  362.         sentencia.executeUpdate(sql);
  363.         System.out.println("Empleados modificado correctamente.");
  364.     }
  365.  
  366.     static void cambiarEdad() throws SQLException {
  367.         System.out.println("    ");
  368.         empleadosTodos();
  369.         System.out.println("Introducir numemp para modificar: ");
  370.         String num = teclado.nextLine();
  371.         System.out.println("Nueva Edad del Empleado " + num + ": ");
  372.         String edad = teclado.nextLine();
  373.         sql = "Update Empleados Set "
  374.                 + "edad = '" + edad + "' "
  375.                 + "Where numemp = '" + num + "';";
  376.         sentencia = con.createStatement();
  377.         sentencia.executeUpdate(sql);
  378.         System.out.println("Empleados modificado correctamente.");
  379.     }
  380.  
  381.     static void cambiarTienda() throws SQLException {
  382.         System.out.println("    ");
  383.         empleadosTodos();
  384.         System.out.println("Introducir numemp para modificar: ");
  385.         String num = teclado.nextLine();
  386.         System.out.println("Nueva Tienda del Empleado " + num + ": ");
  387.         String tienda = teclado.nextLine();
  388.         sql = "Update Empleados Set "
  389.                 + "tienda = '" + tienda + "' "
  390.                 + "Where numemp = '" + num + "';";
  391.         sentencia = con.createStatement();
  392.         sentencia.executeUpdate(sql);
  393.         System.out.println("Empleados modificado correctamente.");
  394.     }
  395.  
  396.     static void cambiarEjerce() throws SQLException {
  397.         System.out.println("    ");
  398.         empleadosTodos();
  399.         System.out.println("Introducir numemp para modificar: ");
  400.         String num = teclado.nextLine();
  401.         System.out.println("Introduce el nuevo trabajo que Ejerce del Empleado " + num + ": ");
  402.         String ejerce = teclado.nextLine();
  403.         sql = "Update Empleados Set "
  404.                 + "ejerce = '" + ejerce + "' "
  405.                 + "Where numemp = '" + num + "';";
  406.         sentencia = con.createStatement();
  407.         sentencia.executeUpdate(sql);
  408.         System.out.println("Empleados modificado correctamente.");
  409.     }
  410.  
  411.     static void cambiarContrato() throws SQLException {
  412.         System.out.println("    ");
  413.         empleadosTodos();
  414.         System.out.println("Introducir numemp para modificar: ");
  415.         String num = teclado.nextLine();
  416.         System.out.println("Nuevo contrato del Empleado " + num + ": ");
  417.         String contrato = teclado.nextLine();
  418.         sql = "Update Empleados Set "
  419.                 + "contrato = '" + contrato + "' "
  420.                 + "Where numemp = '" + num + "';";
  421.         sentencia = con.createStatement();
  422.         sentencia.executeUpdate(sql);
  423.         System.out.println("Empleados modificado correctamente.");
  424.     }
  425.  
  426.     static void cambiarCuota() throws SQLException {
  427.         System.out.println("    ");
  428.         empleadosTodos();
  429.         System.out.println("Introducir numemp para modificar: ");
  430.         String num = teclado.nextLine();
  431.         System.out.println("Nueva Cuota del Empleado " + num + ": ");
  432.         String cuota = teclado.nextLine();
  433.         sql = "Update Empleados Set "
  434.                 + "cuota = '" + cuota + "' "
  435.                 + "Where numemp = '" + num + "';";
  436.         sentencia = con.createStatement();
  437.         sentencia.executeUpdate(sql);
  438.         System.out.println("Empleados modificado correctamente.");
  439.     }
  440.  
  441.     static void cambiarVentas() throws SQLException {
  442.         System.out.println("    ");
  443.         empleadosTodos();
  444.         System.out.println("Introducir numemp para modificar: ");
  445.         String num = teclado.nextLine();
  446.         System.out.println("Nuevas Ventas del Empleado " + num + ": ");
  447.         String ventas = teclado.nextLine();
  448.         sql = "Update Empleados Set "
  449.                 + "ventas = '" + ventas + "' "
  450.                 + "Where numemp = '" + num + "';";
  451.         sentencia = con.createStatement();
  452.         sentencia.executeUpdate(sql);
  453.         System.out.println("Empleados modificado correctamente.");
  454.     }
  455.  
  456.     static void tiendas() throws SQLException {
  457.         Integer op;
  458.         do {
  459.             System.out.println("-----------------------------------\n"
  460.                     + "||Menu Principal                 ||\n"
  461.                     + "-----------------------------------\n"
  462.                     + "|1.Mostrar Tiendas                |\n"
  463.                     + "|2.Añadir Tiendas                 |\n"
  464.                     + "|3.Eliminar Tiendas               |\n"
  465.                     + "|4.Modificar Tiendas              |\n"
  466.                     + "|0.Volver                         |\n"
  467.                     + "-----------------------------------");
  468.             op = teclado.nextInt();
  469.             teclado.nextLine();
  470.             switch (op) {
  471.                 case 1:
  472.                     mostrarTiendas();
  473.                     break;
  474. //                case 2:
  475. //                    añadirTiendas(sentencia, tienda);
  476. //                    break;
  477. //                case 3:
  478. //                    eliminarTiendas(sentencia, tienda);
  479. //                    break;
  480. //                case 4:
  481. //                    modificarTiendas(sentencia, tienda);
  482.                 case 0:
  483.                     System.out.println("Volviendo...\n");
  484.                     break;
  485.                 default:
  486.                     System.out.println("Opcion no valida");
  487.             }
  488.         } while (op != 0);
  489.     }
  490.  
  491.     static void mostrarTiendas() throws SQLException {
  492.         Integer op;
  493.         do {
  494.             System.out.println("-----------------------------------\n"
  495.                     + "||Menu Cosultas Tiendas                   ||\n"
  496.                     + "-----------------------------------\n"
  497.                     + "|1.Mostrar todas las tiendas:             |\n"
  498.                     + "|2.Buscar por ciudad:                     |\n"
  499.                     + "|3.Buscar ventas realizada en tiendas:    |\n"
  500.                     + "|0.Volver.                                |\n"
  501.                     + "-----------------------------------");
  502.             op = teclado.nextInt();
  503.             teclado.nextLine();
  504.             switch (op) {
  505.                 case 1:
  506.                     todasTiendas();
  507.                     break;
  508.                 case 2:
  509.                     busquedaCiudad();
  510.                     break;
  511.                 case 3:
  512.                     tiendasVentas();
  513.                     break;
  514.  
  515.                 case 0:
  516.                     System.out.println("Volviendo...\n");
  517.                     break;
  518.                 default:
  519.                     System.out.println("Opcion no valida");
  520.             }
  521.         } while (op != 0);
  522.     }
  523.  
  524.     static void todasTiendas() throws SQLException {
  525.         try {
  526.  
  527.             sql = "SELECT * FROM Tiendas;";
  528.             sentencia = con.createStatement();
  529.             rs = sentencia.executeQuery(sql);
  530.  
  531.             while (rs.next()) {
  532.                 String res = rs.getString("tienda") + "\t "
  533.                         + rs.getString("ciudad") + "\t"
  534.                         + rs.getString("dir") + "\t"
  535.                         + rs.getString("ventas") + "\t";
  536.                 System.out.println(res);
  537.             }
  538.         } catch (SQLException ex) {
  539.             System.err.println("Error: " + ex);
  540.         }
  541.     }
  542.  
  543.     static void busquedaCiudad() throws SQLException {
  544.         try {
  545.             System.out.println("Se ha establecido la conexión...");
  546.             System.out.println("Introduce la ciudad a mostrar: ");
  547.             String ciu = teclado.nextLine();
  548.  
  549.             sql = "SELECT * FROM Tiendas where ciudad = \'" + ciu + "\';";
  550.             sentencia = con.createStatement();
  551.             rs = sentencia.executeQuery(sql);
  552.             boolean ciudad = false;
  553.             while (rs.next()) {
  554.                 String res = rs.getString("tienda") + "\t "
  555.                         + rs.getString("ciudad") + "\t"
  556.                         + rs.getString("dir") + "\t"
  557.                         + rs.getString("ventas") + "\t";
  558.                 System.out.println(res);
  559.                 ciudad = true;
  560.  
  561.             }
  562.  
  563.             if (ciudad == true) {
  564.                 System.out.println("Ciudad encontrada. Pulse para continuar!");
  565.                 String test = teclado.nextLine();
  566.             } else {
  567.                 System.out.println("La Ciudad no a sido encontrada. Pulse para continuar!");
  568.  
  569.             }
  570.         } catch (SQLException ex) {
  571.             System.err.println("Error: " + ex);
  572.         }
  573.     }
  574.  
  575.     static void tiendasVentas() throws SQLException {
  576.         try {
  577.             System.out.println("Se ha establecido la conexión...");
  578.             System.out.println("Introduce la cantidad de ventas de mayor a menor: ");
  579.             String pre = teclado.nextLine();
  580.  
  581.             sql = "SELECT * FROM Tiendas where ventas <= " + pre + "";
  582.             sentencia = con.createStatement();
  583.             rs = sentencia.executeQuery(sql);
  584.             boolean salida = false;
  585.             while (rs.next()) {
  586.                 String res = rs.getString("tienda") + "\t "
  587.                         + rs.getString("ciudad") + "\t"
  588.                         + rs.getString("dir") + "\t"
  589.                         + rs.getString("ventas") + "\t";
  590.                 System.out.println(res);
  591.                 salida = true;
  592.             }
  593.             if (salida == true) {
  594.                 System.out.println("Cantidad encontrada. Pulse para continuar!");
  595.                 String test = teclado.nextLine();
  596.             } else {
  597.                 System.out.println("La consulta no ha mostrado resultado. Pulse para continuar!");
  598.  
  599.             }
  600.         } catch (SQLException ex) {
  601.             System.err.println("Error: " + ex);
  602.         }
  603.     }
  604. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement