Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void login() {
- String user, passwd = "";
- boolean correcto = false;
- System.out.println("Introduce el correo:");
- while (!correcto) {
- user = read.next();
- if (!user.contains("@") || !user.contains(".")) {
- System.err.println("Introduce un formato de email correcto");
- } else {
- correcto = true;
- if (userExists(user)) {
- //Login
- System.out.println("Introduce la contraseña:");
- passwd = read.next();
- if (checkUserPasswd(user,passwd) != null) {
- Cliente c = checkUserPasswd(user,passwd);
- System.out.println("has entrao atontao");
- }else{
- System.err.println("Password incorrecta, volviendo al menu...");
- }
- } else {
- System.err.println("Este usuario no se encuentra registrado");
- }
- }
- }
- }
- public static boolean userExists(String username) {
- String mailQuery = "SELECT id FROM Cliente WHERE correo = ?";
- ResultSet rs = null;
- Cliente c = null;
- try (
- Connection con = ConnectDB.getInstance();
- PreparedStatement preparedStatement = con.prepareStatement(mailQuery);) {
- preparedStatement.setString(1, username);
- rs = preparedStatement.executeQuery();
- if (rs.next()) {
- return true;
- }
- } catch (SQLException e) {
- System.out.println(e.getErrorCode());
- }finally{
- try {
- ConnectDB.closeConnection();
- rs.close();
- } catch (SQLException ex) {
- Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- return false;
- }
- public static Cliente checkUserPasswd(String username, String passwd){
- String mailQuery = "SELECT * FROM Cliente WHERE correo = ? AND password = ?";
- ResultSet rs = null;
- Cliente c = null;
- try (
- Connection con = ConnectDB.getInstance();
- PreparedStatement preparedStatement = con.prepareStatement(mailQuery);) {
- preparedStatement.setString(1, username);
- preparedStatement.setString(2, passwd);
- rs = preparedStatement.executeQuery();
- if (rs.next()) {
- c = new Cliente();
- c.setId(rs.getInt("id"));
- c.setNombre(rs.getString("nombre"));
- c.setApellido(rs.getString("apellido"));
- c.setDireccion(rs.getString("direccion"));
- c.setCorreo(rs.getString("correo"));
- }
- } catch (SQLException e) {
- System.out.println(e.getErrorCode());
- }finally{
- try {
- ConnectDB.closeConnection();
- rs.close();
- } catch (SQLException ex) {
- Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- return c;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement