Skip to content

Commit

Permalink
Account DAO insert method
Browse files Browse the repository at this point in the history
  • Loading branch information
jmsundin committed Jan 25, 2024
1 parent 389bbef commit 3d58959
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/main/java/DAO/AccountDAO.java
Original file line number Diff line number Diff line change
@@ -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;
}

}

0 comments on commit 3d58959

Please sign in to comment.