我在互联网上搜索了如何从mysql查询中调用的方法获取值,它只返回列中所有行的相同值,我能做什么,以便我得到每行中的相应值当这个方法被调用时。我需要帮助。我的获得结果的方法如下:
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);
}
}
调用的方法是:
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;
}
所有这一切都是为所有员工提供一个相同的PAYE值,我该怎么做才能让它在jtable中显示各自的税值? 请帮助
答案 0 :(得分:1)
您的要求是每位员工返回不同的getTax1()
值。通过向SQL添加getTax1
调用无法实现这一点。您基本上已经完成的是在SELECT
子句中添加一个常量值,并且您确保查询返回的每一行都会有一个常量值添加到字段列表中,这是{{的返回值1}}来电。
为了让它按照您的意愿运行,您需要:添加每个员工计算getTax1()
所需的表达式(使用对您的应用程序有意义的ID加入)或使用以下步骤对每个员工记录进行计算。
在PAYE
方法中:
getResults
您需要更改Execute query to fetch all the details except `PAYE` field that you want to compute.
For each employee in the result
Call `getTax1` with employee ID as parameter to get corresponding value
Add return value to the array that makes the table model.
Done
逻辑以接受Id或其他字段,以便您计算每位员工的需求值。