开发者

mysql using sum and group by clause

I have the following code.The query woks well in mysql but it does not work when integrated with java.Please check the code.Sum is not calculated correctly for all the columns.Please Help .Thanks a lot

import java.io.*;
import java.sql.*;

public class Sum11{

    public static void main(String[] args) {

        System.out.println("Sum of the specific column!");

        Connection con = null;
         int sum1=0;
         int sum2=0;
         int sum3=0;


        try{
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection ("jdbc:mysql://localhost:3306/prathi","root","mysql");
            try{
                Statement st = con.createStatement();
                ResultSet res = st.executeQuery("SELECT  Service_ID,SUM(consumer_feedback) FROM  consumer1 group by Service_ID");
                while (res.next()){
                    int data=res.getInt(1);
                    System.out.println(data);
                    int c1 = res.getInt(2);
                    sum1 = sum1+c1;
                }
                System.out.println("Sum of column "   +sum1);

                while (res.next()){
                    int data=res.getInt(1);
                    System.out.println(data);
                    int c2 = res.getInt(1);
                    sum2 = sum2+c2;
                }
                System.out.println("Sum of column "   +sum2);

                while (res.next()){
                    int data=res.getInt(1);
                    System.out.println(data);
                    int c3 = res.getInt(1);
                    sum3 = sum3+c3;              
                }
                System.out.println("Sum of column "   +sum3);
            }// end of try inner block
            catch (SQLException s){
     开发者_运维知识库           System.out.println("SQL statement is not executed!");
            }
        }
        catch (Exception e){
          e.printStackTrace();
        }
    }
}

I have my table as follows.

+------------+--------------+-------------------+
| Service_ID | Service_Type | consumer_feedback |
+------------+--------------+-------------------+
|         31 | Printer      |                 1 |
|         32 | Printer      |                -1 |
|         32 | Printer      |                -1 |
|         32 | Printer      |                 0 |
|         33 | Printer      |                 1 |
|         33 | Printer      |                 1 |
|         31 | Printer      |                -1 |
|         31 | Printer      |                -1 |


You have 3 while (res.next()) loops working on the same ResultSet. However, the first while loop will iterate over all the rows, and the last 2 while loops won't do anything (because res.next() will be false at that point).

If you can explain why you have 3 loops and what you're trying to do, we can tell you what is wrong with the code.


I might be wrong, but shouldn't the GROUP BY clause have a HAVING clause also?


Looks to me as if you would do the SUM twice, once in SQL and once in Java.

Your source is quite hard to read though.

Why do you have while (res.next()) three times without initializing res in between? Once it returns false in the first loop, it won't be true for the other loops...

If this does not help yet, please reformat your Java code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜