Results 1 to 7 of 7
Like Tree2Likes
  • 2 Post By achill

Thread: calculate making problem while using while() loop in ResultSet

  1. #1
    raj.mscking@gmail.com's Avatar
    raj.mscking@gmail.com is offline Senior Member
    Join Date
    Jan 2012
    Location
    TamilNadu
    Posts
    289
    Rep Power
    3

    Exclamation calculate making problem while using while() loop in ResultSet

    Have A Happy Day To All!

    i wants to calculate the total value of total rows of the particular column (Total)

    this is my java code

    Java Code:
    float total=0.0f;
    
    
    public void getFeeTotal() {
            try {
                Class.forName("com.mysql.jdbc.Driver");
                Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/school", "root", "root");
                PreparedStatement ps100 = conn.prepareStatement("select Total from fees where AdmisonNo=? and Name=?");
                ps100.setString(1, Adno.getText());
                ps100.setString(2, nmm.getText());
                ResultSet rs100 = ps100.executeQuery();
                while (rs100.next()) 
                {
                 
                        float mmm = rs100.getFloat(1);
                        
                        //for(int n=1;n<rs100.getRow();n++)
                        {
                            float temp=mmm;
                             total=mmm+temp; 
                        }
                         System.Out.Println(total)
                }
                 
                conn.close();
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, e);
            }
        }

    values in column of total are

    Java Code:
    Total
    
    25750.0
    800.0
    36000.0
    16200.0
    i wants to calculate the total value of above (Total) field ..,

    what changes i have to do with my code sir..,

    Thank you sir..,

  2. #2
    Ranjiz is offline Member
    Join Date
    Jun 2012
    Posts
    4
    Rep Power
    0

    Default Re: calculate making problem while using while() loop in ResultSet

    while (rs100.next())
    {

    float mmm = rs100.getFloat(1);


    total=total+mmm;


    }
    System.Out.Println(total);

  3. #3
    achill is offline Member
    Join Date
    Jan 2011
    Location
    Bielefeld, Germany
    Posts
    10
    Rep Power
    0

    Default Re: calculate making problem while using while() loop in ResultSet

    change your SQL-Statement to

    Java Code:
    select sum(Total) from fees where AdmisonNo=? and Name=?
    and mySQL calculates the sum for you...
    Tolls and Fubarable like this.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,225
    Rep Power
    20

    Default Re: calculate making problem while using while() loop in ResultSet

    There I was, reading the OP thinking "why not just use SQL?" and all ready to post, and see achill beat me to it...
    :)
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    raj.mscking@gmail.com's Avatar
    raj.mscking@gmail.com is offline Senior Member
    Join Date
    Jan 2012
    Location
    TamilNadu
    Posts
    289
    Rep Power
    3

    Default Re: calculate making problem while using while() loop in ResultSet

    Thank you very much sir!
    i try now self by your idea..,

  6. #6
    raj.mscking@gmail.com's Avatar
    raj.mscking@gmail.com is offline Senior Member
    Join Date
    Jan 2012
    Location
    TamilNadu
    Posts
    289
    Rep Power
    3

    Default Re: calculate making problem while using while() loop in ResultSet

    sir i have no way to thanks you.
    Because you each and every brainpower teach me a lesson by our technology for each time..,
    Thank you! very much, it is working well sir..,

  7. #7
    achill is offline Member
    Join Date
    Jan 2011
    Location
    Bielefeld, Germany
    Posts
    10
    Rep Power
    0

    Default Re: calculate making problem while using while() loop in ResultSet

    You are welcome.

Similar Threads

  1. Problem with ResultSet when using GUI
    By RefugeX in forum New To Java
    Replies: 6
    Last Post: 12-01-2011, 11:21 PM
  2. Replies: 8
    Last Post: 03-10-2011, 04:16 AM
  3. Replies: 2
    Last Post: 01-13-2011, 06:43 AM
  4. Replies: 10
    Last Post: 02-20-2010, 09:25 PM
  5. Problem with ResultSet
    By flaquitqm in forum JDBC
    Replies: 8
    Last Post: 01-27-2010, 09:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •