SHARE
TWEET

Untitled

a guest Jun 12th, 2019 90 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class AccountDAO implements DAO<Account> {
  2.  
  3.     @Override
  4.     public Account get(long id) throws SQLException {
  5.         ResultSet resultSet = DatabaseUtil.REQUEST_RESULT_SET("account WHERE id =" + id);
  6.         Account account = null;
  7.         if (resultSet.next())
  8.          account = new Account(resultSet);
  9.         resultSet.close();
  10.         return account;
  11.     }
  12.  
  13.     @Override
  14.     public List<Account> getAll() throws SQLException {
  15.         List<Account> accounts = new ArrayList<>();
  16.         ResultSet resultSet = DatabaseUtil.REQUEST_RESULT_SET("account");
  17.         while (resultSet.next())
  18.             accounts.add(new Account(resultSet));
  19.         resultSet.close();
  20.         return accounts;
  21.     }
  22.  
  23.     @Override
  24.     public void updateAll(Account account, String[] params) {
  25.         if (!params[0].isEmpty())
  26.             account.setName(params[0]);
  27.         if (!params[1].isEmpty())
  28.             account.setPassword(new DefaultPasswordService().encryptPassword(params[1]));
  29.         if (!params[2].isEmpty())
  30.             account.setAccountRoles(AccountRole.valueOf(params[2]));
  31.         update(account);
  32.     }
  33.  
  34.     @Override
  35.     public void update(Account updated) {
  36.         DatabaseUtil.UPDATE("UPDATE account SET username = '" + updated.getName() + "'," +
  37.                 "password = '" + updated.getPassword() + "'," +
  38.                 "accountRole = '" + updated.getAccountRole().name() + "'" +
  39.                 " WHERE id =" + updated.getId() + ";");
  40.     }
  41.  
  42.     @Override
  43.     public void add(Account account) {
  44.         DatabaseUtil.UPDATE("INSERT INTO account VALUES('" + account.getId() + "','" + account.getName() + "','" +
  45.                 account.getPassword() + "','" + account.getAccountRole().name() + "')");
  46.     }
  47.  
  48.     @Override
  49.     public void delete(long id ) {
  50.         DatabaseUtil.UPDATE("DELETE FROM account WHERE id = '" + id  + "'");
  51.     }
  52. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top