|
| 1 | +package com.stackify.daos; |
| 2 | + |
| 3 | +import java.sql.Connection; |
| 4 | +import java.sql.PreparedStatement; |
| 5 | +import java.sql.ResultSet; |
| 6 | +import java.sql.SQLException; |
| 7 | +import java.util.ArrayList; |
| 8 | +import java.util.List; |
| 9 | + |
| 10 | +import org.apache.logging.log4j.LogManager; |
| 11 | +import org.apache.logging.log4j.Logger; |
| 12 | + |
| 13 | +import com.stackify.models.User; |
| 14 | +import com.stackify.utils.ConnectionUtil; |
| 15 | + |
| 16 | +public class UserDAO { |
| 17 | + |
| 18 | + private Logger logger = LogManager.getLogger(UserDAO.class); |
| 19 | + |
| 20 | + public void createTable() { |
| 21 | + try (Connection con = ConnectionUtil.getConnection()) { |
| 22 | + String createQuery = "CREATE TABLE users(email varchar(50) primary key, name varchar(50))"; |
| 23 | + PreparedStatement pstmt = con.prepareStatement(createQuery); |
| 24 | + |
| 25 | + pstmt.execute(); |
| 26 | + } catch (SQLException exc) { |
| 27 | + logger.error(exc.getMessage()); |
| 28 | + } |
| 29 | + |
| 30 | + } |
| 31 | + |
| 32 | + public void add(User user) { |
| 33 | + try (Connection con = ConnectionUtil.getConnection()) { |
| 34 | + |
| 35 | + String insertQuery = "INSERT INTO users(email,name) VALUES(?,?)"; |
| 36 | + PreparedStatement pstmt = con.prepareStatement(insertQuery); |
| 37 | + pstmt.setString(1, user.getEmail()); |
| 38 | + pstmt.setString(2, user.getName()); |
| 39 | + |
| 40 | + pstmt.executeUpdate(); |
| 41 | + } catch (SQLException exc) { |
| 42 | + logger.error(exc.getMessage()); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + public void update(User user) { |
| 47 | + try (Connection con = ConnectionUtil.getConnection()) { |
| 48 | + |
| 49 | + String updateQuery = "UPDATE users SET name=? WHERE email=?"; |
| 50 | + PreparedStatement pstmt = con.prepareStatement(updateQuery); |
| 51 | + pstmt.setString(1, user.getName()); |
| 52 | + pstmt.setString(2, user.getEmail()); |
| 53 | + |
| 54 | + pstmt.executeUpdate(); |
| 55 | + } catch (SQLException exc) { |
| 56 | + logger.error(exc.getMessage()); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + public void delete(User user) { |
| 61 | + try (Connection con = ConnectionUtil.getConnection()) { |
| 62 | + |
| 63 | + String deleteQuery = "DELETE FROM users WHERE email=?"; |
| 64 | + PreparedStatement pstmt = con.prepareStatement(deleteQuery); |
| 65 | + pstmt.setString(1, user.getEmail()); |
| 66 | + |
| 67 | + pstmt.executeUpdate(); |
| 68 | + } catch (SQLException exc) { |
| 69 | + logger.error(exc.getMessage()); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + public void delete(String email) { |
| 74 | + try (Connection con = ConnectionUtil.getConnection()) { |
| 75 | + |
| 76 | + String deleteQuery = "DELETE FROM users WHERE email=?"; |
| 77 | + PreparedStatement pstmt = con.prepareStatement(deleteQuery); |
| 78 | + pstmt.setString(1, email); |
| 79 | + |
| 80 | + pstmt.executeUpdate(); |
| 81 | + } catch (SQLException exc) { |
| 82 | + logger.error(exc.getMessage()); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + public User findOne(String email) { |
| 87 | + User user = null; |
| 88 | + |
| 89 | + try (Connection con = ConnectionUtil.getConnection()) { |
| 90 | + String query = "SELECT * FROM users WHERE email=?"; |
| 91 | + PreparedStatement pstmt = con.prepareStatement(query); |
| 92 | + pstmt.setString(1, email); |
| 93 | + |
| 94 | + ResultSet rs = pstmt.executeQuery(); |
| 95 | + while (rs.next()) { |
| 96 | + user = new User(); |
| 97 | + user.setEmail(rs.getString("email")); |
| 98 | + user.setName(rs.getString("name")); |
| 99 | + } |
| 100 | + |
| 101 | + } catch (SQLException exc) { |
| 102 | + logger.error(exc.getMessage()); |
| 103 | + } |
| 104 | + |
| 105 | + return user; |
| 106 | + } |
| 107 | + |
| 108 | + public List<User> findAll() { |
| 109 | + List<User> users = new ArrayList<>(); |
| 110 | + |
| 111 | + try (Connection con = ConnectionUtil.getConnection()) { |
| 112 | + String query = "SELECT * FROM users"; |
| 113 | + PreparedStatement pstmt = con.prepareStatement(query); |
| 114 | + |
| 115 | + ResultSet rs = pstmt.executeQuery(); |
| 116 | + while (rs.next()) { |
| 117 | + User user = new User(); |
| 118 | + user.setEmail(rs.getString("email")); |
| 119 | + user.setName(rs.getString("name")); |
| 120 | + users.add(user); |
| 121 | + } |
| 122 | + } catch (SQLException exc) { |
| 123 | + logger.error(exc.getMessage()); |
| 124 | + } |
| 125 | + |
| 126 | + return users; |
| 127 | + } |
| 128 | + |
| 129 | + public void deleteAll() { |
| 130 | + try (Connection con = ConnectionUtil.getConnection()) { |
| 131 | + |
| 132 | + String deleteQuery = "DELETE FROM users"; |
| 133 | + PreparedStatement pstmt = con.prepareStatement(deleteQuery); |
| 134 | + |
| 135 | + pstmt.executeUpdate(); |
| 136 | + } catch (SQLException exc) { |
| 137 | + logger.error(exc.getMessage()); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | +} |
0 commit comments