Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Modelo;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.InputStream;
- import java.sql.*;
- import java.util.Properties;
- import controlador.Controlador;
- import vistas.*;
- public class bbdd {
- private Vista_global vistaGlobal;
- private Controlador controlador;
- private Vista_login vista_login;
- private Vista_singin vista_singin;
- private File config;
- private Properties propiedades;
- private InputStream entrada = null;
- private String bd;
- private String user;
- private String password;
- private String ubicacion;
- private String driver;
- private String url;
- private Connection conexion;
- /**
- * CONSTRUCTOR: establece el nombre del archivo con los datos de configuración
- * Llama a método setDatosAcceso()
- * @param archivo: objeto File.
- * @throws FileNotFoundException
- */
- public bbdd(File archivo) throws FileNotFoundException {
- config = archivo;
- setDatosAcceso();
- }
- /**
- * Establece la conexión con la base de datos.
- */
- public void conexionON(){
- try {
- Class.forName("com.mysql.jdbc.Driver");
- conexion = DriverManager.getConnection(url, user, password);
- } catch (Exception e) {
- e.printStackTrace();
- // Método failDdbbAccess() lanza mensaje de que algo ha ido mal con la conexión
- // y da la posibilidad de modificar los datos del archivo de configuración.
- vista_login.failDdbbAccess();
- }
- }
- /**
- * Cierra la conexión con la base de datos.
- */
- public void conexionOFF(){
- try {
- conexion.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- /* **************************** */
- /* MÉTODO SETTER DE PROPIEDADES */
- /* **************************** */
- public void setVistaGlobal(Vista_global eVistaGlobal) {
- vistaGlobal = eVistaGlobal;
- }
- public void setControlador(Controlador eControlador) {
- controlador = eControlador;
- }
- public void setVista_login(Vista_login eVista_login) {
- vista_login = eVista_login;
- }
- public void setVista_singin(Vista_singin vista_singin) {
- this.vista_singin = vista_singin;
- }
- /**
- * setDatosAcceso()
- * establece los valores de las propiedades de acceso a la bbdd
- *
- * @throws FileNotFoundException
- */
- private void setDatosAcceso() throws FileNotFoundException {
- entrada = new FileInputStream(config);
- propiedades = new Properties();
- try {
- propiedades.load(entrada);
- // Establecer valores de las propiedades de acceso a la bbdd.
- bd = propiedades.getProperty("bd");
- user = propiedades.getProperty("user");
- password = propiedades.getProperty("password");
- ubicacion = propiedades.getProperty("ubicacion");
- driver = propiedades.getProperty("driver");
- url = driver + ubicacion + bd;
- } catch (IOException e1) {
- vista_login.failDdbbAccess();
- }
- }
- /* ******************************************** */
- /* MÉTODOS RELACIONADOS SELECT, INSERT Y UPDATE */
- /* ******************************************** */
- public void setLogeo(String eEmail, String ePassword) throws SQLException {
- conexionON();
- int nIntentos=3;
- if (eEmail.equals("") || ePassword.equals("")) {
- vista_login.showMessage("No debes dejar ningun campo vacío");
- } else {
- try {
- String select = "SELECT * FROM usuarios WHERE email=? AND password=?";
- PreparedStatement stmt = conexion.prepareStatement(select);
- stmt.setString(1, eEmail);
- stmt.setString(2, ePassword);
- ResultSet resultados = stmt.executeQuery();
- resultados.next();
- if (ePassword.equals(resultados.getString("password"))) {
- vista_login.irAlPrograma();
- resultados.close();
- stmt.close();
- } else {
- throw new Exception();
- }
- } catch (Exception e) {
- nIntentos--;
- if (nIntentos == 0) {
- vista_login.cerrarApp();
- } else {
- vista_login.failLogin(nIntentos);
- }
- }
- }
- conexionOFF();
- }
- public void register(String name, String eEmail, String ePassword, String passwordConfirm){
- conexionON();
- boolean cojemos;
- String marcaEmail;
- if (name.equals("") || ePassword.equals("") || eEmail.equals("")
- || passwordConfirm.equals("")) {
- vista_singin.showMessage("Todos los campos son necesarios para completar el registro");
- } else if (!ePassword.equals(passwordConfirm)) {
- vista_singin.showMessage("Las contraseñas deben coincidir para continuar con el registro");
- } else {
- cojemos = false;
- marcaEmail = "";
- for (int i = 0; i < eEmail.length(); i++) {
- if (eEmail.charAt(i) == '@') {
- cojemos = true;
- }
- if (cojemos == true) {
- marcaEmail += eEmail.charAt(i);
- }
- }
- if (cojemos == false) {
- vista_singin.showMessage("La estructura del email debe ser tunombre@tuservidor.es/com");
- } else if (marcaEmail.equals("@hotmail.com")
- || marcaEmail.equals("@hotmail.es")
- || marcaEmail.equals("@gmail.com")
- || marcaEmail.equals("@yahoo.com")) {
- // SI TODO ESTA CORRECTO SE REGISTRA
- System.out.println(eEmail);
- try {
- String comprobar = "SELECT * FROM inventario.usuario WHERE email=?";
- PreparedStatement stmt = conexion.prepareStatement(comprobar);
- stmt.setString(1, eEmail);
- ResultSet resultados = stmt.executeQuery();
- resultados.next();
- String texto = resultados.getString("email");
- vista_singin.showMessage("El email "
- + texto
- + " ya esta registrado en nuestra base de datos \n Utilize otro E-mail");
- resultados.close();
- stmt.close();
- } catch (SQLException e1) {
- // SI SE PRODUCE LA EXCEPCION SE GENERARA EL MENSAJE DE
- // REGISTRO; SINO, SE DIRÁ QUE EL EMAIL YA ESTA REGISTRADO
- // EN LA BBDD
- String insercion = ("INSERT INTO inventario.usuario(nombre,password,email) VALUES ('"
- + name + "','" + ePassword + "','" + eEmail + "')");
- vista_singin.showMessage("El registro se ha completado");
- vista_singin.desactivarVista();
- vista_login.activarVista(vista_singin.getX(),
- vista_singin.getY());
- try {
- PreparedStatement stmt2 = conexion.prepareStatement(insercion);
- stmt2.executeUpdate();
- } catch (SQLException e) {
- vista_singin.showMessage("Fallo al registrarse en la base de datos");
- }
- }
- } else {
- vista_singin.showMessage("Tu email debe ser de los siguientes dominios \n - hotmail.com \n - hotmail.es \n - gmail.com \n - yahoo.com");
- }
- }
- conexionOFF();
- }
- public void getTabla(String tabla) throws SQLException {
- // Establecemos la conexión con la base de datos.
- conexionON();
- String select = "SELECT * FROM " + tabla;
- PreparedStatement stmt = conexion.prepareStatement(select);
- ResultSet resultados = stmt.executeQuery();
- if (tabla.equals("usuario")) {
- while (resultados.next()) {
- int id_usuario = resultados.getInt("id_usuario");
- String nombre = resultados.getString("nombre");
- String password = resultados.getString("password");
- String email = resultados.getString("email");
- vistaGlobal.recibir(id_usuario, nombre, password, email);
- }
- } else if (tabla.equals("equipo")) {
- while (resultados.next()) {
- int id = resultados.getInt("id");
- String placa_base = resultados.getString("placa_base");
- int num_armario = resultados.getInt("num_armario");
- vistaGlobal.recibir(id, placa_base, num_armario);
- }
- } else {
- // MENSAJE DE ERROR
- }
- // Cerramos la conexión con la base de datos:
- conexionOFF();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement