diff --git a/src/main/java/DAO/AccountDAO.java b/src/main/java/DAO/AccountDAO.java new file mode 100644 index 0000000..ec9b2fa --- /dev/null +++ b/src/main/java/DAO/AccountDAO.java @@ -0,0 +1,37 @@ +package DAO; + +import Model.Account; + +import Util.ConnectionUtil; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; + +public class AccountDAO { + + public Account insertAccount(Account account){ + Connection connection = ConnectionUtil.getConnection(); + try { + String sql = "INSERT INTO account (username, password) VALUES (?, ?)"; + PreparedStatement ps = connection.prepareStatement(sql); + + //add code that leverages ps.setString here + ps.setString(1, account.getUsername()); + ps.setString(2, account.getPassword()); + + 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()); + } + + }catch(SQLException e){ + e.printStackTrace(); + } + return null; + } + +}