简体   繁体   中英

Error: Before start of result set in Java

I know this would be a foolish question to ask but still i need to do this. This is a basic program in java application where I want to use 3 queries simultaneously to print the table.

(I'm not using any Primary key in this case so please help me to resolve this without making my attributes as primary keys - I know this is not a good practice but for now i need to complete it.)

my code:

    Connection con = null;        
    Statement stat1 = null, stat2 = null, stat3 = null;
    ResultSet rs1, rs2, rs3;
    stat1 = con.createStatement();
    stat2 = con.createStatement();
    stat3 = con.createStatement();

    String str = "\nProduct\tC.P\tS.P.\tStock\tExpenditure\tSales";
    info.setText(str);

    String s1 = "SELECT type, cp, sp, stock FROM ts_items GROUP BY type ORDER BY type";
    String s2 = "SELECT expenditure FROM ts_expenditure GROUP BY type ORDER BY type";
    String s3 = "SELECT sales FROM ts_sales GROUP BY type ORDER BY type";
    rs1 = stat1.executeQuery(s1);  
    rs2 = stat2.executeQuery(s2); 
    rs3 = stat3.executeQuery(s3); 

    String type;
    int cp, sp, stock, expenditure, sales;

   while( rs1.next() || rs2.next() || rs3.next() )
   {

       type = rs1.getString("type");
       cp = rs1.getInt("cp");
       sp = rs1.getInt("sp");
       stock = rs1.getInt("stock");

       expenditure = rs2.getInt("expenditure");

       sales = rs3.getInt("sales");

       info.append("\n" + type + "\t" + cp + "\t" + sp + "\t" + stock + "\t" + expenditure + "\t" + sales);


   }

Output:

Runtime Exception: Before start of result set 

This is the problem:

while( rs1.next() || rs2.next() || rs3.next() )

If rs1.next() returns true , rs2.next() and rs3.next() won't be called due to short-circuiting. So rs2 and rs3 will both be before the first row. And if rs1.next() returns false, then you couldn't read from that anyway...

I suspect you actually want:

while (rs1.next() && rs2.next() && rs3.next())

After all, you only want to keep going while all three result sets have more information, right?

It's not clear why you're not doing an appropriate join, to be honest. That would make a lot more sense to me... Then you wouldn't be trying to use multiple result sets on a single connection, and you wouldn't be relying on there being the exact same type values in all the different tables.

You do an OR so imagine only one ResultSet has a result.

What you end up with is trying to read from empty result sets.

Suppose rs1 has one result and rs3 has 3 results. Now as per your code it will fail for rs1.getString("type"); during second iteration. Better to loop over each resultSet separately.

This is going to go badly wrong, in the event that there is a type value that's missing from one of your three tables. Your code just assumes you'll get all of the types from all of the tables. It may be the case for your current data set, but it means that your code is not at all robust.

I would seriously recommend having just one SQL statement, that has each of your three selects as subselects, then joins them all together. Your java can just iterate over the result from this one SQL statement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM