From e6210edd099208a4de160b17c721c2edef71b86e Mon Sep 17 00:00:00 2001 From: Soka_jplr Date: Sun, 10 Mar 2024 16:11:32 -0600 Subject: [PATCH] creacion de Controladores y cambios en Vista y Modelo --- Base/cocina.sql | 5 +- nbproject/project.properties | 2 +- src/Controlador/Controlador.java | 13 ------ src/Controlador/ControladorLogin.java | 54 ++++++++++++++++++++++ src/Controlador/ControladorRegistro.java | 56 +++++++++++++++++++++++ src/{Vista => }/Images/music.png | Bin src/Main/Main.java | 5 ++ src/Modelo/Conexion.java | 48 +++++++++++++++++++ src/Modelo/Consulta.java | 55 ++++++++++++++++++++++ src/Modelo/Modelo.java | 13 ------ src/Modelo/Usuario.java | 6 +++ src/Vista/Vista.java | 10 ---- src/Vista/seleccionarBoleto.java | 52 +-------------------- 13 files changed, 231 insertions(+), 88 deletions(-) delete mode 100644 src/Controlador/Controlador.java create mode 100644 src/Controlador/ControladorLogin.java create mode 100644 src/Controlador/ControladorRegistro.java rename src/{Vista => }/Images/music.png (100%) create mode 100644 src/Modelo/Conexion.java create mode 100644 src/Modelo/Consulta.java delete mode 100644 src/Modelo/Modelo.java delete mode 100644 src/Vista/Vista.java diff --git a/Base/cocina.sql b/Base/cocina.sql index 0349c0a..711f5c6 100644 --- a/Base/cocina.sql +++ b/Base/cocina.sql @@ -1,6 +1,9 @@ create database cocina; - use cocina; +#Crear un usuario para ocuparlo en el programa y no estar cambiando el archivo conexion.java +CREATE USER 'UserRemoto' @'localhost' IDENTIFIED BY 'password123'; +GRANT ALL PRIVILEGES ON cocina TO 'UserRemoto' @'localhost'; +FLUSH PRIVILEGES; drop table usuarios; create table usuarios( diff --git a/nbproject/project.properties b/nbproject/project.properties index af96f54..eff1744 100644 --- a/nbproject/project.properties +++ b/nbproject/project.properties @@ -73,7 +73,7 @@ jlink.additionalmodules= jlink.additionalparam= jlink.launcher=true jlink.launcher.name=SalaDeConciertos -main.class= +main.class=Main.Main manifest.file=manifest.mf meta.inf.dir=${src.dir}/META-INF mkdist.disabled=false diff --git a/src/Controlador/Controlador.java b/src/Controlador/Controlador.java deleted file mode 100644 index b1c3a9e..0000000 --- a/src/Controlador/Controlador.java +++ /dev/null @@ -1,13 +0,0 @@ -/* - * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license - * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template - */ -package Controlador; - -/** - * - * @author juanl - */ -public class Controlador { - -} diff --git a/src/Controlador/ControladorLogin.java b/src/Controlador/ControladorLogin.java new file mode 100644 index 0000000..1816c92 --- /dev/null +++ b/src/Controlador/ControladorLogin.java @@ -0,0 +1,54 @@ + +package Controlador; + +import Modelo.Consulta; +import Modelo.Usuario; +import Vista.Login; +import Vista.Registro; +import Vista.SalaPrincipal; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import javax.swing.JOptionPane; + +/* + * @author Juan Ska +*/ +public class ControladorLogin implements ActionListener{ + private Login login; + private Consulta consulta = new Consulta(); + public ControladorLogin(Login login) { + this.login = login; + } + + @Override + public void actionPerformed(ActionEvent e) { + //Valora que boton presiono el Usuario. + if(e.getSource()== login.getBtnLogin()){ + String valorPass = new String(login.getTxtContraseñaLogin().getPassword()); + //Valora si los campos etan vacios + if(login.getTxtNombreUsuario().getText().isEmpty()||valorPass.isEmpty()){ + JOptionPane.showMessageDialog(null, "Error. Favor de llenar Todos los campos"); + }else{ + //Busca al usuario en la base de datos + Usuario user = new Usuario(login.getTxtNombreUsuario().getText(), valorPass); + if(consulta.verificarUsuario(user)){ + //Abre una nueva ventana + JOptionPane.showMessageDialog(null, "Bienvenido " + user.getNombre()); + SalaPrincipal principal = new SalaPrincipal(user); + principal.setVisible(true); + login.dispose(); + }else{ + JOptionPane.showMessageDialog(null, "Error. Nombre de usuario o Contraseña incorrectas"); + } + } + } + //Abre la ventana de registro + if(e.getSource()== login.getBtnRegistrarse()){ + Registro registro = new Registro(); + registro.setLocationRelativeTo(null); + registro.setVisible(true); + login.dispose(); + } + } + +} diff --git a/src/Controlador/ControladorRegistro.java b/src/Controlador/ControladorRegistro.java new file mode 100644 index 0000000..81b6d4b --- /dev/null +++ b/src/Controlador/ControladorRegistro.java @@ -0,0 +1,56 @@ +package Controlador; + +import Modelo.Consulta; +import Vista.Registro; +import java.awt.Color; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.KeyEvent; +import java.awt.event.KeyListener; + +/* + * @author Juan Ska +*/ +public class ControladorRegistro extends Thread{ + private Registro registro; + private String name=""; + private Consulta consulta = new Consulta(); + public ControladorRegistro(Registro registro) { + this.registro = registro; + run(); + } + @Override + public void run(){ + registro.getBtnRegistrar().addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody + } + }); + + registro.getTxtNombreUsuarioRegistro().addKeyListener(new KeyListener() { + @Override + public void keyTyped(KeyEvent e) { + } + + @Override + public void keyPressed(KeyEvent e) { + } + + @Override + public void keyReleased(KeyEvent e) { + name = registro.getTxtNombreUsuarioRegistro().getText(); + if(consulta.usuarioInvalido(name)){ + registro.getLabelName().setForeground(Color.red); + registro.getLabelName().setText("No puedes usar ese nombre"); + registro.getBtnRegistrar().setEnabled(false); + }else{ + registro.getLabelName().setForeground(Color.GREEN); + registro.getLabelName().setText("Nombre Disponible"); + registro.getBtnRegistrar().setEnabled(true); + } + } + }); + } + +} diff --git a/src/Vista/Images/music.png b/src/Images/music.png similarity index 100% rename from src/Vista/Images/music.png rename to src/Images/music.png diff --git a/src/Main/Main.java b/src/Main/Main.java index 9eb367d..3c1260c 100644 --- a/src/Main/Main.java +++ b/src/Main/Main.java @@ -1,8 +1,13 @@ package Main; +import Vista.Login; + public class Main { public static void main(String[] args) { + Login login = new Login(); + login.setLocationRelativeTo(null); + login.setVisible(true); } } \ No newline at end of file diff --git a/src/Modelo/Conexion.java b/src/Modelo/Conexion.java new file mode 100644 index 0000000..26e10ed --- /dev/null +++ b/src/Modelo/Conexion.java @@ -0,0 +1,48 @@ +package Modelo; +import java.sql.*; + +public class Conexion { + + private static Connection conexion; + private static Conexion istancia; + private static final String url = "jdbc:mysql://localhost:3306/cocina"; + private static final String user = "UserRemoto"; + private static final String password ="password123"; + + private Conexion(){} + + public Connection conectar(){ + try { + Class.forName("com.mysql.cj.jdbc.Driver"); + conexion = DriverManager.getConnection(url, user, password); + if(conexion!=null){ + System.out.println("Conexion Exitosa"); + return conexion; + }else{ + System.out.println("ERROR no se pudo conectar"); + } + } catch (Exception e) { + System.out.println("ERROR: " + e); + } + return conexion; + } + public void cerrarconexion() throws SQLException{ + try { + conexion.close(); + System.out.println("Se desconecto de la base"); + } catch (Exception e) { + System.out.println("ERROR: " + e); + conexion.close(); + }finally{ + conexion.close(); + } + } + + public static Conexion getInstance(){ + if(istancia==null){ + istancia = new Conexion(); + } + return istancia; + } + +} \ No newline at end of file diff --git a/src/Modelo/Consulta.java b/src/Modelo/Consulta.java new file mode 100644 index 0000000..c4ba64a --- /dev/null +++ b/src/Modelo/Consulta.java @@ -0,0 +1,55 @@ +package Modelo; + +import java.sql.*; +import java.sql.ResultSet; +import java.sql.SQLException; + + +public class Consulta { + private static final Conexion cn = Conexion.getInstance(); + private static Connection conexion; + public Consulta(){} + + public boolean verificarUsuario(Usuario user){ + boolean verificacion =false; + try { + conexion = cn.conectar(); + Statement stm; + String sql ="select * from Usuarios " + + "where nombreUsuario= '"+user.getNombre()+"' and password='"+user.getPassword()+"'"; + stm = conexion.createStatement(); + ResultSet resultado = stm.executeQuery(sql); + if(resultado.next()){ + verificacion = true; + }else{ + verificacion = false; + } + cn.cerrarconexion(); + } catch (SQLException ex) { + System.err.println(ex); + } + return verificacion; + } + + public boolean usuarioInvalido(String name) { + boolean verificacion =false; + try { + conexion = cn.conectar(); + Statement stm; + String sql ="select * from Usuarios " + + "where nombreUsuario= '"+name+"'"; + stm = conexion.createStatement(); + ResultSet resultado = stm.executeQuery(sql); + if(resultado.next()){ + verificacion = true; + }else{ + verificacion = false; + } + cn.cerrarconexion(); + } catch (SQLException ex) { + System.err.println(ex); + } + return verificacion; + } + +} diff --git a/src/Modelo/Modelo.java b/src/Modelo/Modelo.java deleted file mode 100644 index e4e40b0..0000000 --- a/src/Modelo/Modelo.java +++ /dev/null @@ -1,13 +0,0 @@ -/* - * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license - * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template - */ -package Modelo; - -/** - * - * @author juanl - */ -public class Modelo { - -} diff --git a/src/Modelo/Usuario.java b/src/Modelo/Usuario.java index 4783749..9624f11 100644 --- a/src/Modelo/Usuario.java +++ b/src/Modelo/Usuario.java @@ -17,6 +17,12 @@ public class Usuario { public Usuario(){ } + + public Usuario(String nombre, String password) { + this.nombre = nombre; + this.password = password; + } + public String getNombre() { return nombre; diff --git a/src/Vista/Vista.java b/src/Vista/Vista.java deleted file mode 100644 index b850cb7..0000000 --- a/src/Vista/Vista.java +++ /dev/null @@ -1,10 +0,0 @@ - -package Vista; - -/** - * - * @author juanl - */ -public class Vista { - -} diff --git a/src/Vista/seleccionarBoleto.java b/src/Vista/seleccionarBoleto.java index 87b7075..0dc7595 100644 --- a/src/Vista/seleccionarBoleto.java +++ b/src/Vista/seleccionarBoleto.java @@ -1,27 +1,14 @@ -/* - * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license - * Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JFrame.java to edit this template - */ package Vista; - /** * * @author reyes */ -public class seleccionarBoleto extends javax.swing.JFrame { +public class SeleccionarBoleto extends javax.swing.JFrame { - /** - * Creates new form seleccionarBoleto - */ - public seleccionarBoleto() { + public SeleccionarBoleto() { initComponents(); } - /** - * This method is called from within the constructor to initialize the form. - * WARNING: Do NOT modify this code. The content of this method is always - * regenerated by the Form Editor. - */ @SuppressWarnings("unchecked") // //GEN-BEGIN:initComponents private void initComponents() { @@ -46,7 +33,6 @@ public class seleccionarBoleto extends javax.swing.JFrame { jButton1 = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); - setMaximumSize(new java.awt.Dimension(32767, 32767)); jPanel1.setBackground(new java.awt.Color(249, 244, 244)); @@ -206,40 +192,6 @@ public class seleccionarBoleto extends javax.swing.JFrame { pack(); }// //GEN-END:initComponents - /** - * @param args the command line arguments - */ - public static void main(String args[]) { - /* Set the Nimbus look and feel */ - // - /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. - * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html - */ - try { - for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { - if ("Nimbus".equals(info.getName())) { - javax.swing.UIManager.setLookAndFeel(info.getClassName()); - break; - } - } - } catch (ClassNotFoundException ex) { - java.util.logging.Logger.getLogger(seleccionarBoleto.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); - } catch (InstantiationException ex) { - java.util.logging.Logger.getLogger(seleccionarBoleto.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); - } catch (IllegalAccessException ex) { - java.util.logging.Logger.getLogger(seleccionarBoleto.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); - } catch (javax.swing.UnsupportedLookAndFeelException ex) { - java.util.logging.Logger.getLogger(seleccionarBoleto.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); - } - // - - /* Create and display the form */ - java.awt.EventQueue.invokeLater(new Runnable() { - public void run() { - new seleccionarBoleto().setVisible(true); - } - }); - } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton jButton1;