Q:

java programming and sql dividing sum of two column

      try{
       String sum="",sum1="";
          String sql = "select sum(unit) from Result where idNumber=?";
          pst=conn.prepareStatement(sql);
          pst.setString(1,issearchTextField.getText());
          rs=pst.executeQuery();
          if(rs.next()){
           sum = rs.getString("sum(unit)");

          }

          String sql1 = "select sum(ugp) from Result where idNumber=?";
          PreparedStatement pst1=conn.prepareStatement(sql1);
          pst1.setString(1,issearchTextField.getText());
           ResultSet rs1=pst.executeQuery();
          if(rs1.next()){
           sum1 = rs1.getString("sum(ugp)");

          }

          String rst = (sum)/(sum1);
         ccnameTextField.setText(rst);
      }
      catch(Exception ex){

      }
       finally{
    try{
        rs.close();
        pst.close();
    }
    catch(Exception e){
    }
    }

/* goodday everyone, please i'm trying to get the sum of two columns
       in the database,and divide these sums which will be an output to  textField,below is what i tried. please help me...*/

A:

You can do that in single query 
select sum(unit)/NULLIF(sum(ugp),0)
from Result 
where idNumber=?

NULLIF is used to avoid Divide by zero error 

