(sum)在jtext中查询结果

时间:2016-09-17 22:25:13

标签: java sql-server

我想创建一个函数来向我展示sum中的Jtextfield。我不知道如何调用结果? 我的jtextfieldnamesum_calc .Ty

public void getSum() {

             Connection con =getConnection();
             Statement st;
             PreparedStatement pst = null;
             ResultSet rs;
            try{
                String qquery=" SELECT sum(Receipt.Sales_Quantity * Receipt.Pro_Price) AS TotalItemsOrdered FROM Receipt";
              pst=con.prepareStatement(qquery);
                             pst.setString(1,sum_calc );
                             rs=pst.executeQuery();

           if(rs.next()){
            JOptionPane.showMessageDialog(null, qquery);

            }else{
                JOptionPane.showMessageDialog(null, "zero");
            }

        }catch (Exception e) { JOptionPane.showMessageDialog(null, e); }

    };

以下是调用函数的button

 private void make_Receipt_sumActionPerformed(java.awt.event.ActionEvent evt) {                                                 
        getSum();
}

1 个答案:

答案 0 :(得分:0)

  public void getSum() {

         Connection con =getConnection();
         Statement st;
         PreparedStatement pst = null;
         ResultSet rs;
        try{
            String qquery=" SELECT sum(Receipt.Sales_Quantity * Receipt.Pro_Price) AS TotalItemsOrdered FROM Receipt";
          pst=con.prepareStatement(qquery);
                         //pst.setString(1,qquery );
                         rs=pst.executeQuery();

         if(rs.next()){
              float TotalItemsOrdered = rs.getFloat("TotalItemsOrdered");
              float nvNum=TotalItemsOrdered;
             sum_calc.setText(valueOf(nvNum));
       JOptionPane.showMessageDialog(null, rs);

         }else{
            JOptionPane.showMessageDialog(null, "Not Found");
        }

    }catch (Exception e) { JOptionPane.showMessageDialog(null, e); }

}