【问题标题】:Calling a java method in Mysql query-java在Mysql query-java中调用java方法
【发布时间】:2015-09-15 02:58:28
【问题描述】:

我在互联网上搜索了如何从 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 中显示各自的税值? 请帮忙

【问题讨论】:

  • 所以基本上你想在JTable 上针对每个员工显示getTax1 的返回值?看看getTax1 方法,无论我们对哪个员工感兴趣,它都会返回相同的值 - 对吗?
  • 那么您遇到的错误是什么,或者它是否有效?是st.executeQuerySQLException
  • @ring,我没有收到任何错误,它会为第一个员工选择价值并将其放入所有其他员工中。我希望它为每个员工征税并显示PAYE 列中的每一行
  • 是的,我希望它针对每个员工 @ring 显示返回值

标签: java mysql jtable


【解决方案1】:

您的要求是为每位员工返回不同的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 或其他字段,以便您计算每个员工的所需值。

【讨论】:

  • ,请原谅,我怎样才能用 id 调用 getax1,请不要介意...如果可能的话,一个示例
  • @kazisto :根据您的评论,getTax1 会因员工而异。这意味着getTax1 中的查询应该包含一些特定于员工的条件。目前getTax1 中的查询是静态的——无论如何它都会返回相同的值。所以你需要重新设计那个查询。那时,您将清楚需要将哪些值作为动态值传递给查询。它们应该是您的方法签名的一部分。 HTH
  • 先生,我应该使用哪种条件?我已经尝试过“.....where alloweds.Name=money.Name”,但它显示了同样的东西......
  • @kazisto :) 我无法就此提供建议,因为我对您的数据模型一无所知。
  • 刚刚被困在我的项目中...请帮助
猜你喜欢
  • 2015-05-07
  • 2012-09-28
  • 2013-10-01
  • 2013-04-03
  • 2012-03-22
  • 2011-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多