From 1d7a6441cf42f2d2a681bc43620372110abff132 Mon Sep 17 00:00:00 2001 From: Jon Sundin Date: Sun, 28 Jan 2024 08:38:33 +0000 Subject: [PATCH] Account DAO methods --- src/main/java/DAO/AccountDAO.java | 66 +++++++++++++++++++++++++------ 1 file changed, 54 insertions(+), 12 deletions(-) diff --git a/src/main/java/DAO/AccountDAO.java b/src/main/java/DAO/AccountDAO.java index ec9b2fa..22a939d 100644 --- a/src/main/java/DAO/AccountDAO.java +++ b/src/main/java/DAO/AccountDAO.java @@ -8,30 +8,72 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.sql.Statement; + +import java.util.List; +import java.util.ArrayList; public class AccountDAO { - public Account insertAccount(Account account){ - Connection connection = ConnectionUtil.getConnection(); + public Account insertAccount(String username, String password){ try { - String sql = "INSERT INTO account (username, password) VALUES (?, ?)"; - PreparedStatement ps = connection.prepareStatement(sql); + Connection conn = ConnectionUtil.getConnection(); + String sql = "INSERT INTO account (username, password_) VALUES (?, ?)"; + PreparedStatement ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); - //add code that leverages ps.setString here - ps.setString(1, account.getUsername()); - ps.setString(2, account.getPassword()); + ps.setString(1, username); + ps.setString(2, password); ps.executeUpdate(); ResultSet pkeyResultSet = ps.getGeneratedKeys(); - if (pkeyResultSet.next()){ - int generated_account_id = (int) pkeyResultSet.getLong(1); - return new Account(generated_account_id, account.getUsername(), account.getPassword()); + while (pkeyResultSet.next()) { + Account account = new Account(username, password); + account.setAccount_id((int) (pkeyResultSet.getLong(1))); + return account; } + } catch(SQLException e){ + e.printStackTrace(); + } + return null; + } + + public Account findAccountByUsername(String username) { + try { + Connection conn = ConnectionUtil.getConnection(); + String sql = "SELECT account_id, username, password_ " + + "FROM account WHERE username = ?"; + PreparedStatement ps = conn.prepareStatement(sql); + ps.setString(1, username); - }catch(SQLException e){ + ResultSet rs = ps.executeQuery(); + if (rs.next()) { + String usernameFromDB = rs.getString(2); + String password = rs.getString(3); + return new Account(usernameFromDB, password); + } + } catch (SQLException e) { e.printStackTrace(); } return null; } - + + public List getAllAccounts() { + List accounts = new ArrayList<>(); + try { + Connection conn = ConnectionUtil.getConnection(); + String sql = "SELECT * FROM account"; + PreparedStatement ps = conn.prepareStatement(sql); + ResultSet rs = ps.executeQuery(); + while (rs.next()) { + String username = rs.getString(2); + String password = rs.getString(3); + Account account = new Account(username, password); + accounts.add(account); + System.out.println(account); + } + } catch (SQLException e) { + e.printStackTrace(); + } + return accounts; + } }