在Mysql查询java中调用java方法
我在互联网上搜索过如何从mysql查询中调用的方法中获取值,该方法只返回一列中所有行的相同值,如何才能在调用该方法时获取每行中的相应值。我需要帮助。我获取结果的方法如下:在Mysql查询java中调用java方法,java,mysql,jtable,Java,Mysql,Jtable,我在互联网上搜索过如何从mysql查询中调用的方法中获取值,该方法只返回一列中所有行的相同值,如何才能在调用该方法时获取每行中的相应值。我需要帮助。我获取结果的方法如下: public final void getResults() { // from = (String) empol.getSelectedItem(); resultsTable=new JTable(); model=new DefaultTableModel(); int i; int
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);
}
}
它所做的只是为所有员工提供了一个相同的PAYE值,我该怎么做才能让它在jtable中显示各自的纳税值?
请帮助您的要求是为每位员工返回不同的
getTax1()
值。这不能通过向SQL添加getTax1
调用来实现。实际上,您所做的是在SELECT
子句中添加一个常量值,从而确保查询返回的每一行都会向字段列表中添加一个常量值,即getTax1()
调用的返回值
为了使其按您的意愿工作,您需要:添加计算每个员工的PAYE
所需的表达式(使用对您的应用程序有意义的ID加入)或使用以下步骤计算每个员工的记录
在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
您需要更改
getTax1
逻辑以接受一个Id或其他字段,这些字段允许您计算每个员工的require值。因此基本上您希望在JTable
上显示每个员工的getTax1
返回值?看看这个方法getTax1
,不管我们对哪个员工感兴趣,它都会返回相同的值-正确吗?那么您得到的错误是什么,或者它正在工作?是st.executeQuery
抛出SQLException
?@ring,我没有收到任何错误,它所做的一切它为第一个员工选择值,并将其放入所有其他员工。我希望它为PAYE列中每一行的每一位员工获取税收并显示是的,我希望它显示每个员工@ring的返回值,请原谅,我如何使用id调用getax1,请不要介意…如果可能,asample@kazisto:根据您的评论,每个员工的getTax1
都会更改。这意味着getTax1
中的查询应该包含特定于员工的一些条件。当前,getTax1
中的查询是一种静态的-无论发生什么,它都将返回相同的值。因此,您需要重新设计该查询。此时,您就可以清楚地知道需要将哪些值作为动态值传递给查询。它们应该是方法签名的一部分。HTH我应该使用哪种条件,先生?我已经尝试过“…where amberties.Name=money.Name”,但它显示了相同的东西…@kazisto:)我不能就此提供建议,因为我对您的数据模型一无所知。只是困在我的项目中…请帮助
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