The problem isn't that I got 2 queries (that is required) but the problem I have is there is 2 method executing very similar queries and it just seems kind of pointless.
Like, I have this method:
public void addData() {
DecimalFormat dfForm = new DecimalFormat("$0.00");
DateFormat dateFormat = new SimpleDateFormat("MMMM d, yyyy");
try {
ResultSet rs = null;
double dAmount = 0.0;
// select information showing all the transactions for the groups
String sQuery = "SELECT group_trans.trans_type, group_trans.amount, " +
" group_trans.date, group_trans.mint_id, customers.homeArea, customers.homePrefix, " +
"customers.homeLine, group_trans.PaymentType FROM group_trans " +
"INNER JOIN customers ON customers.cust_id = group_trans.mint_id " +
"WHERE group_trans.group_id =" + nGrpId + "" +
" ORDER BY group_trans.trans_id " +
"";
rs = menu.dbConn.results(sQuery);
if (!rs.next()) {
return; // no results in the database
}
// add the data from the database to the table.
// start the indices at 1 because the indices in the result set
// start at 1. The table indices start 0 which is why we subtract one.
for (int x = 1; x <= nRows; x++) {
for (int y = 1; y <= nCols; y++) {
if (y == 1) {
if (rs.getString("trans_type").equals("Credit")) {
data[x-1][y-1] = dfForm.format(rs.getDouble("amount"));
data[x-1][y] = "-";
dAmount += rs.getDouble("amount");
}
} else if (y == 2) {
if (rs.getString("trans_type").equals("Payment")) {
data[x-1][y-1] = dfForm.format(rs.getDouble("amount"));
data[x-1][y-2] = "-";
dAmount -= rs.getDouble("amount");
}
} else if (y == 3) {
data[x-1][y-1] = dateFormat.format(rs.getDate("date"));
} else if (y == 4) {
if (rs.getString("mint_id").equals("-1")) {
data[x-1][y-1] = "-";
} else {
data[x-1][y-1] = rs.getString("homeArea") + rs.getString("homePrefix") + rs.getString("homeLine");
}
} else if (y == 5) {
data[x-1][y-1] = dfForm.format(dAmount);
} else if (y == 6) {
// show the payment type
if (!rs.getString("PaymentType").equals("Cash") && !rs.getString("PaymentType").equals("-")) {
data[x-1][y-1] = "Cheque # " + rs.getString("PaymentType");
} else {
data[x-1][y-1] = rs.getString("PaymentType");
}
}
}
rs.next(); // go to the next row in the result set
}
rs.close();
} catch (SQLException ex) {
errors.sqlError(ex);
}
}
Which executes one query and displays it in a table. Then I have another method that selects all the transactions between 2 dates and displays them in another table.
private Vector<Vector> getTrans() {
Vector<Vector> rows = new Vector<Vector>();
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
// get all the credit for this group before startDate
double dBalance = grpControl.getCredits(startDate.getDate(),nGrpId);
String sQuery = "SELECT group_trans.trans_type, group_trans.amount, " +
" group_trans.date, customers.homeArea, customers.homePrefix, " +
"customers.homeLine, group_trans.PaymentType FROM group_trans " +
"INNER JOIN customers ON customers.cust_id = group_trans.mint_id " +
"WHERE group_trans.group_id =" + nGrpId + " " +
"AND group_trans.date BETWEEN '" + dateFormat.format(startDate.getDate()) +
"' AND '" + dateFormat.format(dateEnd.getDate()) + "' ORDER BY group_trans.date ASC , " +
"group_trans.trans_id ASC";
dateFormat = new SimpleDateFormat("MMMM d, yyyy");
try {
ResultSet rs = menu.dbConn.results(sQuery);
while (rs.next()) {
Vector row = new Vector();
if (rs.getString("trans_type").equals("Payment")) {
// debits
row.add("-");
row.add(dfForm.format(rs.getDouble("amount")));
dBalance -= rs.getDouble("amount");
} else {
// credits
row.add(dfForm.format(rs.getDouble("amount")));
row.add("-");
dBalance += rs.getDouble("amount");
}
row.add(dateFormat.format(rs.getDate("date")));
row.add(rs.getString("homeArea") + rs.getString("homePrefix") + rs.getString("homeLine"));
row.add(dfForm.format(dBalance));
if (!rs.getString("PaymentType").equals("Cash") && !rs.getString("PaymentType").equals("-")) {
row.add("Cheque # " + rs.getString("PaymentType"));
} else {
row.add(rs.getString("PaymentType"));
}
rows.add(row);
}
return rows;
} catch (SQLException ex) {
errors.sqlError(ex);
return null;
}
}
See, the redundancy? The getTrans method passes the vector back and then it is iterated and displayed in the table using a second method very similar to the method I posted first. Thus every time we limit transactions we are iterating twice which is slow and makes my job a lot harder.
I'm just not sure on how I can make one method that handles selecting all transactions and one that selects only transactions between 2 dates. Thoughts?