AccountDAO

mail@pastecode.io avatar
unknown
plain_text
a month ago
6.0 kB
0
Indexable
Never
/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package DAOs;

import Models.Account;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author MSI GTX
 */
public class AccountDAO {

    private PreparedStatement ps;
    private Connection conn;
    private ResultSet rs;

    public AccountDAO() {
        conn = DB.DbConnection.GetConnection();
    }

    //chuyển mã mật khẩu sang md5
    public String encryptToMD5(String password) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] messageDigest = md.digest(password.getBytes());

            StringBuilder hexString = new StringBuilder();
            for (byte b : messageDigest) {
                String hex = Integer.toHexString(0xFF & b);
                if (hex.length() == 1) {
                    hexString.append('0');
                }
                hexString.append(hex);
            }
            return hexString.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return null;
            
        }
    }

    public boolean login(Account tk) throws SQLException {
        rs = null;
        String sql = "select * from Account WHERE email = ? AND password = ?";
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1, tk.getEmail());
            String enterdPassword = encryptToMD5(tk.getPassword());
            ps.setString(2, enterdPassword);
            rs = ps.executeQuery();
        } catch (Exception ex) {
            Logger.getLogger(AccountDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return rs.next();
    }

    public int AddAccount(Account ac) {
        String sql = "Insert into Account values(?, ?, ?, ?, ?, ?)";
        int kq = 0;
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1, ac.getFullname());
            ps.setString(2, ac.getEmail());
            ps.setString(3, ac.getAddress());
            ps.setInt(4, ac.getPhone());
            ps.setString(5, ac.getPassword());
            ps.setInt(6, ac.getPin());
            kq = ps.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(AccountDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return kq;
    }

    public Account GetAccount(String email) {
        String sql = "select * from Account where email=?";
        Account sv = null;
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1, email); // lay gia tri id va the vao 1 dau cham hoi
            rs = ps.executeQuery(); // dung khi su dung select

            if (rs.next()) {
                sv = new Account(rs.getString("fullname"), rs.getString("password"), rs.getString("email"));
            }
        } catch (SQLException ex) {
            Logger.getLogger(AccountDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return sv;
    }

    public boolean checkemail(String email) {
        String sql = "SELECT * FROM Account WHERE email = ?";
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1, email);
            rs = ps.executeQuery();
            return rs.next(); // Trả về true nếu email tồn tại, ngược lại trả về false
        } catch (SQLException ex) {
            Logger.getLogger(AccountDAO.class.getName()).log(Level.SEVERE, null, ex);
            return false; // Trong trường hợp xử lý lỗi, trả về false
        }
    }
    // gửi email đến khách hang

    public int Update(Account aac) {
        int ketqua = 0;
        String sql = "update Account set password=? where email=?";
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, aac.getPassword());
            ps.setString(2, aac.getEmail());
            ketqua = ps.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(AccountDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return ketqua;
    }

    public boolean checkpin(Account Account) throws SQLException {
        rs = null;
        String sql = "select * from Account WHERE pin=?";
        try {
            ps = conn.prepareStatement(sql);
            ps.setInt(1, Account.getPin());
            rs = ps.executeQuery();
        } catch (Exception ex) {
            Logger.getLogger(AccountDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return rs.next();
    }
    public int updatePin(Account Account){
        int ketqua = 0;
        String sql = "UPDATE Account SET pin=? WHERE email=?";
        try {
            ps = conn.prepareStatement(sql);
            ps.setInt(1, Account.getPin());
            ps.setString(2, Account.getEmail());
            ketqua = ps.executeUpdate();
        } catch (Exception ex) {
            Logger.getLogger(AccountDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return ketqua;
    }
        public int DeleteAccount(String email) {
        String sql = "delete from Account where email=?";
        int ketqua = 0;

        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, email);
            ketqua = ps.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(AccountDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return ketqua;
    }

}
Leave a Comment