Advertisement
Guest User

Untitled

a guest
Jan 16th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.21 KB | None | 0 0
  1. public static void login() {
  2.  
  3.         String user, passwd = "";
  4.         boolean correcto = false;
  5.  
  6.         System.out.println("Introduce el correo:");
  7.         while (!correcto) {
  8.             user = read.next();
  9.             if (!user.contains("@") || !user.contains(".")) {
  10.                 System.err.println("Introduce un formato de email correcto");
  11.             } else {
  12.                 correcto = true;
  13.                 if (userExists(user)) {
  14.                     //Login
  15.                     System.out.println("Introduce la contraseña:");
  16.                     passwd = read.next();
  17.                     if (checkUserPasswd(user,passwd) != null) {
  18.                         Cliente c = checkUserPasswd(user,passwd);
  19.                         System.out.println("has entrao atontao");
  20.                     }else{
  21.                         System.err.println("Password incorrecta, volviendo al menu...");
  22.                     }
  23.  
  24.                 } else {
  25.                     System.err.println("Este usuario no se encuentra registrado");
  26.                 }
  27.  
  28.             }
  29.         }
  30.  
  31.     }
  32.  
  33.     public static boolean userExists(String username) {
  34.         String mailQuery = "SELECT id FROM Cliente WHERE correo = ?";
  35.         ResultSet rs = null;
  36.         Cliente c = null;
  37.         try (
  38.             Connection con = ConnectDB.getInstance();
  39.             PreparedStatement preparedStatement = con.prepareStatement(mailQuery);) {
  40.             preparedStatement.setString(1, username);
  41.             rs = preparedStatement.executeQuery();
  42.             if (rs.next()) {
  43.                 return true;
  44.             }
  45.         } catch (SQLException e) {
  46.             System.out.println(e.getErrorCode());
  47.         }finally{
  48.             try {
  49.                 ConnectDB.closeConnection();
  50.                 rs.close();
  51.             } catch (SQLException ex) {
  52.                 Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
  53.             }
  54.         }
  55.         return false;
  56.     }
  57.    
  58.     public static Cliente checkUserPasswd(String username, String passwd){
  59.         String mailQuery = "SELECT * FROM Cliente WHERE correo = ? AND password = ?";
  60.         ResultSet rs = null;
  61.         Cliente c = null;
  62.         try (
  63.             Connection con = ConnectDB.getInstance();
  64.             PreparedStatement preparedStatement = con.prepareStatement(mailQuery);) {
  65.             preparedStatement.setString(1, username);
  66.             preparedStatement.setString(2, passwd);
  67.             rs = preparedStatement.executeQuery();
  68.             if (rs.next()) {
  69.                 c = new Cliente();
  70.                 c.setId(rs.getInt("id"));
  71.                 c.setNombre(rs.getString("nombre"));
  72.                 c.setApellido(rs.getString("apellido"));
  73.                 c.setDireccion(rs.getString("direccion"));
  74.                 c.setCorreo(rs.getString("correo"));
  75.             }
  76.         } catch (SQLException e) {
  77.             System.out.println(e.getErrorCode());
  78.         }finally{
  79.             try {
  80.                 ConnectDB.closeConnection();
  81.                 rs.close();
  82.             } catch (SQLException ex) {
  83.                 Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
  84.             }
  85.         }
  86.         return c;
  87.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement