generated from revature-curriculum/pep-project
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
37 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |