For a project, I am trying to read a table containing transactions. I use the following code:
Code:
/**
* 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;
}
My problem is that my SQLite query returns a single line, whereas when I manually browse the table using sqlite3, I have dozens of transactions. How is this possible? Am I only one or it is common?
Bookmarks