lundi 22 février 2016

How to use dao factory pattern for login and registration dao

I am using separate DAO classes for login and registration as follows:

Registration DAO:

public class RegistrationDAO {

    public void insert(UserProfile user) {
        try {
            Connection con = DBConnection.getConnection();
            String query = "insert into TBL_USER (USR_FIRST_NAME,USR_LST_NAME,USR_PRIMARY_EMAIL,USR_PASSWORD) values(?,?,?,?)";
            PreparedStatement pst = con.prepareStatement(query);

            pst.setString(1, user.getFirstName());
            pst.setString(2, user.getLastName());
            pst.setString(3, user.getEmail());
            pst.setString(4, user.getPassword());
            pst.executeUpdate();
        } catch (Exception e) {
            System.out.println("@@@@Record insertion error in Registration DAO@@@@");
            e.printStackTrace();
        }
    }
}

Login DAO:

public class LoginDAO {

    public boolean authenticate(String email, String password)
            throws Exception {
        boolean isUser = false;
        try {
            Connection con = DBConnection.getConnection();
            PreparedStatement statement = con.prepareStatement("select USR_PRIMARY_EMAIL, USR_PASSWORD from TBL_USER where USR_PRIMARY_EMAIL=? and USR_PASSWORD=?");
            statement.setString(1, email);
            statement.setString(2, password);
            ResultSet result = statement.executeQuery();
            if (result.next()) {
                isUser = true;
                System.out.println("User authenticated successfully");
            } else {
                System.out.println("Invalid username or password!");
            }
        } catch (Exception e) {
            System.out.println("DB related Error");
            e.printStackTrace();
        }
        return isUser;
    }
}

Lets say if I change my database from oracle to mySql in future then I should be able to connect. I came to know that DAO factory and DAO interface must be used to connect to multiple databases, but I am not able to figure how to apply it in my case.

Do I need to have two DAO interfaces for RegistatrationDao and LoginDao respectively? what is better way of achieving this?

Aucun commentaire:

Enregistrer un commentaire