AccountDAO
unknown
plain_text
a year ago
4.3 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(); } 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()); kq = ps.executeUpdate(); } catch (SQLException ex) { Logger.getLogger(AccountDAO.class.getName()).log(Level.SEVERE, null, ex); } return kq; } 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 } } // public boolean checkemail(String email) { // boolean emailExists = false; // String sql = "select * from Account where email = ?"; // try { // ps = conn.prepareStatement(sql); // ps.setString(1, email); // rs = ps.executeQuery(); // if (rs.next()) { // emailExists = true; // } // } catch (SQLException ex) { // // Xử lý lỗi khác (nếu có) // ex.printStackTrace(); // } catch (Exception ex) { // ex.printStackTrace(); // } finally { // try { // if (rs != null) { // rs.close(); // } // if (ps != null) { // ps.close(); // } // if (conn != null) { // conn.close(); // } // } catch (SQLException ex) { // ex.printStackTrace(); // } // } // return emailExists; // } }