Untitled
unknown
plain_text
3 years ago
3.7 kB
6
Indexable
/*
* 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 EncodeMD5.MD5;
import Models.Account;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author trant
*/
public class AccountDAO {
private Connection conn;
private PreparedStatement ps;
private ResultSet rs;
public AccountDAO() {
conn = DBConnection.DbConnection.getConnection();
}
public boolean Login(Account acc) throws SQLException {
ResultSet rs = null;
String sql = "SELECT * FROM account WHERE username=? AND password=?";
try {
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, acc.getUsername());
ps.setString(2, MD5.encode(acc.getPassword())); // Mã hóa mật khẩu với MD5
// In ra tài khoản và mật khẩu để debug
System.out.println("Username: " + acc.getUsername());
System.out.println("Password: " + acc.getPassword());
rs = ps.executeQuery();
} catch (Exception ex) {
Logger.getLogger(AccountDAO.class.getName()).log(Level.SEVERE, null, ex);
}
return rs.next();
}
public List<Account> getAll() {
List<Account> accounts = new ArrayList<>();
String sql = "SELECT * FROM account";
try {
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
Account acc = new Account(
rs.getString("username"),
rs.getString("password"),
rs.getString("fullname"),
rs.getString("gender"),
rs.getDate("birthdate"),
rs.getString("department")
);
accounts.add(acc);
}
} catch (SQLException ex) {
Logger.getLogger(AccountDAO.class.getName()).log(Level.SEVERE, null, ex);
}
return accounts;
}
public String GetFullName(String username){
String fullname = null;
try {
ps = conn.prepareStatement("select fullname from account where username=?");
ps.setString(1, username);
rs = ps.executeQuery();
if(rs.next()){
fullname = rs.getString("fullname");
}
} catch (SQLException ex) {
Logger.getLogger(DBConnection.DbConnection.class.getName()).log(Level.SEVERE, null, ex);
}
return fullname;
}
public Account getInformation(String username) {
Account acc = null;
try {
ps = conn.prepareStatement("select * from account where username=?"); // ? de chong hack
ps.setString(1, username);
rs = ps.executeQuery();
if (rs.next()) { // neu co dong du lieu thi next sang dong du lieu do
acc = new Account(rs.getString("username"), rs.getString("password"), rs.getString("fullname")
, rs.getString("gender"), rs.getDate("birthdate"), rs.getString("derpartment"));
}
return acc;
} catch (SQLException ex) {
Logger.getLogger(AccountDAO.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
}
Editor is loading...