1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| /**
* Import pending transactions records from the database
* @return HashMap containing deposit and associated account
*/
public HashMap<Deposit, Account> importPendingTransactions() {
HashMap<Deposit, Account> pendingTransactions = new HashMap<Deposit, Account>();
Statement stat = null;
try {
stat = dbConnection.createStatement();
ResultSet transactionRS = stat.executeQuery("SELECT * FROM " + PENDING_TRANSACTION_TABLE + ";");
while (transactionRS.next()) {
// Convert SQL Date to Java one.
Date transactionDate = new Date(transactionRS.getDate(TRANSACTION_DATE).getTime());
Deposit depositTransaction = new Deposit(transactionRS.getFloat(TRANSACTION_AMOUNT), transactionDate);
ResultSet accountRs = stat.executeQuery("SELECT * FROM " + ACCOUNT_TABLE + " WHERE " + ACCOUNT_ID + "=" + transactionRS.getString(TRANSACTION_ACCOUNT) + ";");
Account userAccount = null;
while (accountRs.next()) {
userAccount = new Account(accountRs.getInt(ACCOUNT_ID), accountRs.getString(ACCOUNT_NUMBER), Account.ACCOUNT_TYPE.CREDITCARD_ACCOUNT, accountRs.getFloat(ACCOUNT_BALANCE));
}
accountRs.close();
if (userAccount != null) {
// Add transaction & account couple to the hashmap.
pendingTransactions.put(depositTransaction, userAccount);
}
}
// Close Accounts Result Set
transactionRS.close();
} catch (SQLException e) {
Logger.getAnonymousLogger().log(Level.SEVERE, "Failed to execute SQL request to import pending transactions: " + e.getLocalizedMessage());
return null;
} finally {
if (stat != null) {
try {
stat.close();
} catch (SQLException ex) {
// Ignore exceptions in finally
}
}
}
Logger.getAnonymousLogger().log(Level.INFO, pendingTransactions.size() + " pending transactions imported.");
return pendingTransactions;
} |
Partager