George Kontonikolaou George Kontonikolaou - 2 months ago 8
Java Question

(Sum) query results in jtext

i want to create a function to show me the

sum
in
Jtextfield
.I dont know how to call the result any idea ?
My
jtextfieldname
is
sum_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); }

};


Here is the
button
that call the function

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

Answer
  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); }

}