java - 在Mysql query-java中调用java方法

标签 java mysql jtable

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

最佳答案

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

关于java - 在Mysql query-java中调用java方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31102255/

相关文章:

java - 如何删除默认边框以及如何锚定 jtable 的列标题?

java - java中的静态内存是怎么回事?

java - 我可以使用在 Java 数组中调用的方法的参数吗

Java Swing - JTable 中的多个列标题?

Mysql 双连接 SUM 发票总额和付款

php - 无法在 CodeIgniter 中向 PHP 数组添加数据

java - 更新由 netbeans 创建的 jTable 上的某些条目

java - Struts2 验证问题

java - 意外错误 : java. security.InvalidAlgorithmParameterException:trustAnchors 参数必须为非空

php - Doctrine 2 映射引用唯一键