Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package proyectoyomusico;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.Scanner;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- /**
- *
- * @author Cristian
- */
- public class ProyectoYomusico {
- static Scanner teclado = new Scanner(System.in);
- static String sentenciaSQL;
- static com.mysql.jdbc.Connection con;
- static String sql = null;
- static Statement sentencia;
- static ResultSet rs;
- static String empleado = null;
- static String consulta = null;
- 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", "1234");
- 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.Salir |\n"
- + "-----------------------------------");
- op = teclado.nextInt();
- teclado.nextLine();
- if (op == null) {
- }
- switch (op) {
- case 1:
- mostrarEmpleados(sentencia, empleado);
- break;
- case 2:
- añadirEmpleados(sentencia, empleado);
- break;
- case 3:
- eliminarEmpleados(sentencia, empleado);
- break;
- case 4:
- modificarEmpleados(sentencia, empleado);
- case 0:
- System.out.println("Volviendo...\n");
- break;
- default:
- System.out.println("Opcion no valida");
- }
- } while (op != 0);
- }
- static void mostrarEmpleados(Statement sentencia, String empleado) throws SQLException {
- Integer op;
- do {
- System.out.println("-----------------------------------\n"
- + "||Menu Modificación Empleados ||\n"
- + "-----------------------------------\n"
- + "|1.Buscar por Nombre: |\n"
- + "|2.Buscar por Tienda: |\n"
- + "|3.Busqueda por ventas: |\n"
- + "|0.Volver. |\n"
- + "-----------------------------------");
- op = teclado.nextInt();
- teclado.nextLine();
- if (op == null) {
- }
- switch (op) {
- case 1:
- EmpleadosTodos(sentencia, empleado);
- break;
- case 2:
- EmpleadosTiendas(sentencia, empleado);
- break;
- case 3:
- EmpleadosVentas(sentencia, empleado);
- break;
- case 0:
- System.out.println("Volviendo...\n");
- break;
- default:
- System.out.println("Opcion no valida");
- }
- } while (op != 0);
- }
- static void EmpleadosTodos(Statement sentencia, String empleado) throws SQLException {
- try {
- System.out.println("Se ha establecido la conexión...");
- sql = "SELECT * FROM Empleados;";
- sentencia = con.createStatement();
- rs = sentencia.executeQuery(sql);
- 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);
- }
- } catch (SQLException ex) {
- System.err.println("Error: " + ex);
- }
- }
- static void EmpleadosTiendas(Statement sentencia, String empleado) throws SQLException {
- try {
- System.out.println("Se ha establecido la conexión...");
- sql = "SELECT * FROM Empleados where order by tienda;";
- sentencia = con.createStatement();
- rs = sentencia.executeQuery(sql);
- 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);
- }
- } catch (SQLException ex) {
- System.err.println("Error: " + ex);
- }
- }
- static void EmpleadosVentas(Statement sentencia, String empleado) throws SQLException {
- try {
- System.out.println("Se ha establecido la conexión...");
- sql = "SELECT * FROM Empleados where order by tienda;";
- sentencia = con.createStatement();
- rs = sentencia.executeQuery(sql);
- 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);
- }
- } catch (SQLException ex) {
- System.err.println("Error: " + ex);
- }
- }
- static boolean existeEmpleados(Statement sentencia, String empleado) {
- boolean existe = false;
- String s = "SELECT * FROM Empleado WHERE nombre=\'" + empleado + "\'";
- ResultSet rs;
- try {
- rs = sentencia.executeQuery(s);
- if (rs.next()) {
- existe = true;
- } else {
- existe = false;
- }
- } catch (SQLException ex) {
- }
- return existe;
- }
- static void añadirEmpleados(Statement sentencia, String empleado) throws SQLException {
- System.out.println("Introduce el numemp (número id): ");
- String num = teclado.nextLine();
- 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();
- empleado = "INSERT INTO Empleados VALUE ('" + num + "','" + nom + "','" + edad + "','" + tien + "','" + ejer + "','" + contrato + "' ,'" + cuota + "','" + ventas + "');";
- sentencia = con.createStatement();
- sentencia.executeUpdate(empleado);
- System.out.println("Empleado registrado");
- String test = teclado.nextLine();
- }
- static void eliminarEmpleados(Statement sentencia, String empleado) throws SQLException {
- // BORRA UN EMPLEADO
- mostrarEmpleados(sentencia, empleado);
- System.out.println("Introducir el numemp del Empleado para borrar: ");
- String numemp = teclado.nextLine();
- String 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(Statement sentencia, String empleado) 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.Salir |\n"
- + "-----------------------------------");
- op = teclado.nextInt();
- teclado.nextLine();
- if (op == null) {
- }
- switch (op) {
- case 1:
- cambiarNombre(sentencia, empleado);
- break;
- case 2:
- cambiarEdad(sentencia, empleado);
- break;
- case 3:
- cambiarTienda(sentencia, empleado);
- break;
- case 4:
- cambiarEjerce(sentencia, empleado);
- break;
- case 5:
- cambiarContrato(sentencia, empleado);
- break;
- case 6:
- cambiarCuota(sentencia, empleado);
- break;
- case 7:
- cambiarVentas(sentencia, empleado);
- break;
- case 0:
- System.out.println("Volviendo...\n");
- break;
- default:
- System.out.println("Opcion no valida");
- }
- } while (op != 0);
- {
- }
- }
- static void cambiarNombre(Statement sentencia, String empleado) throws SQLException {
- System.out.println(" ");
- mostrarEmpleados(sentencia, empleado);
- System.out.println("Introducir numemp para modificar: ");
- String num = teclado.nextLine();
- System.out.println("Nuevo nombre del Empleado " + num + ": ");
- String nombre = teclado.nextLine();
- empleado = "Update Empleados Set "
- + "nombre = '" + nombre + "' ";
- sentencia = con.createStatement();
- sentencia.executeUpdate(empleado);
- System.out.println("Empleados modificado correctamente.");
- }
- static void cambiarEdad(Statement sentencia, String empleado) throws SQLException {
- System.out.println(" ");
- mostrarEmpleados(sentencia, empleado);
- System.out.println("Introducir numemp para modificar: ");
- String num = teclado.nextLine();
- System.out.println("Nueva Edad del Empleado " + num + ": ");
- String edad = teclado.nextLine();
- empleado = "Update Empleados Set "
- + "edad = '" + edad + "' ";
- sentencia = con.createStatement();
- sentencia.executeUpdate(empleado);
- System.out.println("Empleados modificado correctamente.");
- }
- static void cambiarTienda(Statement sentencia, String empleado) throws SQLException {
- System.out.println(" ");
- mostrarEmpleados(sentencia, empleado);
- System.out.println("Introducir numemp para modificar: ");
- String num = teclado.nextLine();
- System.out.println("Nueva Tienda del Empleado " + num + ": ");
- String tienda = teclado.nextLine();
- empleado = "Update Empleados Set "
- + "tienda = '" + tienda + "' ";
- sentencia = con.createStatement();
- sentencia.executeUpdate(empleado);
- System.out.println("Empleados modificado correctamente.");
- }
- static void cambiarEjerce(Statement sentencia, String empleado) throws SQLException {
- System.out.println(" ");
- mostrarEmpleados(sentencia, empleado);
- 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();
- empleado = "Update Empleados Set "
- + "ejerce = '" + ejerce + "' ";
- sentencia = con.createStatement();
- sentencia.executeUpdate(empleado);
- System.out.println("Empleados modificado correctamente.");
- }
- static void cambiarContrato(Statement sentencia, String empleado) throws SQLException {
- System.out.println(" ");
- mostrarEmpleados(sentencia, empleado);
- System.out.println("Introducir numemp para modificar: ");
- String num = teclado.nextLine();
- System.out.println("Nuevo contrato del Empleado " + num + ": ");
- String contrato = teclado.nextLine();
- empleado = "Update Empleados Set "
- + "contrato = '" + contrato + "' ";
- sentencia = con.createStatement();
- sentencia.executeUpdate(empleado);
- System.out.println("Empleados modificado correctamente.");
- }
- static void cambiarCuota(Statement sentencia, String empleado) throws SQLException {
- System.out.println(" ");
- mostrarEmpleados(sentencia, empleado);
- System.out.println("Introducir numemp para modificar: ");
- String num = teclado.nextLine();
- System.out.println("Nueva Cuota del Empleado " + num + ": ");
- String cuota = teclado.nextLine();
- empleado = "Update Empleados Set "
- + "cuota = '" + cuota + "' ";
- sentencia = con.createStatement();
- sentencia.executeUpdate(empleado);
- System.out.println("Empleados modificado correctamente.");
- }
- static void cambiarVentas(Statement sentencia, String empleado) throws SQLException {
- System.out.println(" ");
- mostrarEmpleados(sentencia, empleado);
- System.out.println("Introducir numemp para modificar: ");
- String num = teclado.nextLine();
- System.out.println("Nuevas Ventas del Empleado " + num + ": ");
- String ventas = teclado.nextLine();
- empleado = "Update Empleados Set "
- + "ventas = '" + ventas + "' ";
- sentencia = con.createStatement();
- sentencia.executeUpdate(empleado);
- System.out.println("Empleados modificado correctamente.");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement