i have searched on the internet on how i can get values from a method called in mysql query, whereby it only returns identical values for all rows in a column,what can i do so that i get the respective values in each row when the method is called.I need help please.My method for getting results is as follows:
public final void getResults() {
// from = (String) empol.getSelectedItem();
resultsTable=new JTable();
model=new DefaultTableModel();
int i;
int count;
String a[];
String header[] = { "Name","Date","Basic","Commision","Allowances","NSSF","Deductions","PAYE","Gross","Net"}; //Table Header Values, change, as your wish
count = header.length;
for(i = 0; i < count; i++) {
model.addColumn(header[i]);
}
resultsTable.setModel(model);
docwin.add(resultsTable.getTableHeader(),BorderLayout.NORTH);
a = new String[count];
try {
dbconn.connect();
st = dbconn.conn.createStatement();
SQL = "select money.Name, money.Date, money.earnings,money.commision,(SELECT SUM(Amount) "
+ " FROM allowances where allowances.Name=money.Name) as Allowance,(select nssf_amount from nhif where "
+"nhif.Name=money.Name ) as NSSF, (SELECT SUM(Amount) "
+ " FROM deductions where deductions.Name=money.Name ) as deductions,'"+getTax1()+"' as Paye, "
+ " (SELECT (earnings +commision + (SELECT SUM(Amount) "
+ " FROM allowances where allowances.Name=money.Name))) as GrossPay,"
+ "(SELECT ((SELECT (earnings +commision + (SELECT SUM(Amount) "
+ " FROM allowances where allowances.Name=money.Name)))) - "
+ "((SELECT SUM(Amount)"
+ " FROM deductions where deductions.Name=money.Name )+(SELECT nhif_amount FROM nhif where nhif.Name=money.Name )+ "
+ "(earnings * 0.16)+(select nssf_amount from nhif where "
+"nhif.Name=money.Name ) ) ) as NetPay "
+ "from money Group by Name order by money.monid ";
rs = st.executeQuery(SQL);
while (rs.next()){
for(i = 0; i < count; i++){
a[i] = rs.getString(i+1);
}
model.addRow(a); //Adding the row in table model
resultsTable.setModel(model); // set the model in jtable
}
pane = new JScrollPane(resultsTable);
docwin.add(pane);
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, ex);
}
}
the method that is invoked is this:
public String getTax1() throws SQLException {
String result="";
String s = "select (money.earnings + money.commision+(select sum(Amount) from allowances where allowances.Name=money.Name)) as Total from money ";
Statement sts;
ResultSet rst;
sts = dbconn.conn.createStatement();
rst = sts.executeQuery(s);
while (rst.next()) {
result = rst.getString("Total");
}
// return result;
int gross= Integer.parseInt(result);
final double ftx1 = 10165;
final double ftx2 = 9576;
final double ftx3 = 9576;
final double ftx4 = 9576;
final double ftx5 = 0;
// the percentage of fedral tax
final double taxper1 = 0.10;
final double taxper2 = 0.15;
final double taxper3 = 0.20;
final double taxper4 = 0.25;
final double taxper5 = 0.30;
Double relief=1162.0;
// double theincome = in.nextDouble();
//1st cut
double thing1 = Math.min(ftx1, gross);
double taxd1 = thing1 * taxper1;
double thinga = Math.max(gross - ftx1, 0);
//2nd cut
double thing2 = Math.min(ftx2, thinga);
double taxd2 = thing2 * taxper2;
double thingb = Math.max(thinga - ftx2, 0);
//3rd cut
double thing3 = Math.min(ftx3, thingb);
double taxd3 = thing3 * taxper3;
double thingc = Math.max(thingb - ftx3, 0);
//4th cut
double thing4 = Math.min(ftx4, thingc);
double taxd4 = thing4 * taxper4;
double thingd = Math.max(thingc - ftx4, 0);
//5th cut
double thing5 = Math.max(ftx5, thingd);
double taxd5 = thing5 * taxper5;
//total federal tax-tft
double tft = taxd1 + taxd2 + taxd3 + taxd4 + taxd5;
double tax=tft-relief;
netTax1=Double.toString(tax);
return netTax1;
}
All it does is that it gives one identical PAYE value for all employees,what can i do to make it show respective tax values in the jtable? kindly help
getTax1against each employee on theJTable? And looking at the methodgetTax1it is going to return same value no matter what employee we are interested in - correct?st.executeQuerythrowingSQLException?