login/password data validation

Asked

Viewed 40 times

-1

created 2 classes one of connection and the other

how I could do a login/password validation with these two classes

connection

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package semeq;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author SpiriT
 */
public class ConnectionFactory {

private static final String DRIVER = "com.mysql.jdbc.Driver";
private static final String URL = "jdbc:mysql://localhost:3306/helpsemeq";
private static final String USER = "root";
private static final String PASS = "";

public static Connection getConnection(){

    try {
        Class.forName(DRIVER);
        return DriverManager.getConnection(URL, USER, PASS);     

    } catch (ClassNotFoundException | SQLException ex) {
       throw new RuntimeException("Erro na conexão: ",ex);
    }
}

public static void closeConnection(Connection con){

        try {
            if(con != null){
            con.close();
        } 

        }catch (SQLException ex) {
             Logger.getLogger(ConnectionFactory.class.getName()).log(Level.SEVERE, null, ex);
        }
    }


public static void closeConnection(Connection con, PreparedStatement stmt){

    closeConnection(con);

        try {
            if(stmt != null){
            stmt.close();
        } 

        }catch (SQLException ex) {
            Logger.getLogger(ConnectionFactory.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

public static void closeConnection(Connection con, PreparedStatement stmt, ResultSet rs){

    closeConnection(con, stmt);

        try {
            if(rs != null){
            rs.close();
        } 

        }catch (SQLException ex) {
            Logger.getLogger(ConnectionFactory.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

dao

package semeq;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class daoSemeq {
private ResultSet rs;
private Statement stm;
private Connection conn = ConnectionFactory.getConnection();

public daoSemeq (){

}
public ResultSet getUserDAO () throws SQLException{
    this.stm = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
    this.rs = stm.executeQuery("SELECT login,senha FROM usuário");

    return this.rs;
}
}

my doubt and how I would cosneguiria to do a validation in the form to check the login and the password entered.

1 answer

0

Dude, I don’t understand what your DAO class is referring to, but come on... For starters obviously if you want to do a loguin you have to have the loguin information in the database... I recommend not saving the password visibly there, since anyone with access to the database will be able to see all users and their passwords... recommend using an MD5 or any other encryption... But come on. For the User class the check could be simple>>

public boolean verificarLoguin(Usuario u) {
    String sql = "SELECT * FROM usuario WHERE loguin =" + u.getLoguin();
    PreparedStatement stmt = null;
    ResultSet rs = null;
    try {
        stmt = con.prepareStatement(sql);
        rs = stmt.executeQuery();
        if (rs.first()) {
            if (rs.getString("senha").equals(u.getSenha())) {
                return true;
            }
        }
        return false;
    } catch (SQLException ex) {
        Logger.getLogger(DAO.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        ConnnectionFactory.closeConnection(con, stmt, rs);
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.