From 3d58959915e0b64f56a39364ed833bad044c6dc5 Mon Sep 17 00:00:00 2001 From: Jon Sundin Date: Thu, 25 Jan 2024 23:47:39 +0000 Subject: [PATCH] Account DAO insert method --- src/main/java/DAO/AccountDAO.java | 37 +++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 src/main/java/DAO/AccountDAO.java 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; + } + +}