Models
public class User {
private int id;
private String email;
private String password;
private String role;
public User(String email, String password, String role) {
super();
this.email = email;
this.password = password;
this.role = role;
}
public User(int id, String email, String password, String role) {
super();
this.id = id;
this.email = email;
this.password = password;
this.role = role;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public Order order() {
return Database.getInstance().getOrderDao().find(id);
}
public List<Order> getOrders() {
return DatabaseDao.getInstance().getOrderDao().findByUser(id);
}
}
Interface DAO
public interface UserDAO {
public boolean insert(User user);
public boolean update(User user);
public boolean delete(int email);
public User find(int id);
public List<User> findAll();
public User find(String email, String password);
public User find(String email);
}
Implements
public class UserImpl implements UserDAO {
Connection con = MySQLDriver.getInstance().getConnection();
@Override
public boolean insert(User user) {
// TODO Auto-generated method stub
String sql = "INSERT INTO USERS(ID, EMAIL, PASSWORD, ROLE) VALUES(null, ?, ?, ?)";
try {
PreparedStatement stmt = con.prepareStatement(sql);
stmt.setString(1, user.getEmail());
stmt.setString(2, user.getPassword());
stmt.setString(3, user.getRole());
stmt.execute();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
@Override
public boolean update(User user) {
// TODO Auto-generated method stub
String sql = "UPDATE USERS SET email = ? ,password = ?, role = ? WHERE id = ?";
try {
PreparedStatement stmt = con.prepareStatement(sql);
stmt.setString(1, user.getEmail());
stmt.setString(2, user.getPassword());
stmt.setString(3, user.getRole());
stmt.setInt(4, user.getId());
return stmt.execute();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
@Override
public boolean delete(int id) {
// TODO Auto-generated method stub
String sql = "DELETE FROM USERS WHERE id = ?";
try {
PreparedStatement stmt = con.prepareStatement(sql);
stmt.setInt(1, id);
return stmt.execute();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
@Override
public User find(int id) {
String sql = "SELECT * FROM USERS WHERE ID = ?";
try {
PreparedStatement stmt = con.prepareStatement(sql);
stmt.setInt(1, id);
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
String email = rs.getString("email");
String password = rs.getString("password");
String role = rs.getString("role");
return new User(id, email, password, role);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
public List<User> findAll() {
List<User> users = new ArrayList<>();
String sql = "SELECT * FROM Users WHERE ID > ? ";
try {
PreparedStatement stmt = con.prepareStatement(sql);
stmt.setInt(1, 0);
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
int id = rs.getInt("id");
String email = rs.getString("email");
String password = rs.getString("password");
String role = rs.getString("role");
users.add(new User(id, email, password, role));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return users;
}
@Override
public User find(String email, String password) {
String sql = "SELECT * FROM USERS WHERE EMAIL = ? AND PASSWORD = ?";
try {
PreparedStatement stmt = con.prepareStatement(sql);
stmt.setString(1, email);
stmt.setString(2, password);
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
int id = rs.getInt("id");
String role = rs.getString("role");
return new User(id, email, password, role);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
public User find(String email) {
String sql = "SELECT * FROM USERS WHERE EMAIL = ?";
try {
PreparedStatement stmt = con.prepareStatement(sql);
stmt.setString(1, email);
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
int id = rs.getInt("id");
String password = rs.getString("password");
String role = rs.getString("role");
return new User(id, email, password, role);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}