简体   繁体   English

错误:在Java中开始结果集之前

[英]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. 这是Java应用程序中的一个基本程序,我想同时使用3个查询来打印表。

(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. 如果rs1.next()返回truers2.next()由于短路而不会调用rs2.next()rs3.next() So rs2 and rs3 will both be before the first row. 因此rs2rs3都将在第一行之前。 And if rs1.next() returns false, then you couldn't read from that anyway... 而且如果rs1.next()返回false,那么无论如何您都无法从中读取...

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. 这对我来说意义更大……然后,您将不会尝试在单个连接上使用多个结果集,也不会依赖于所有不同表中存在完全相同的type值。

You do an OR so imagine only one ResultSet has a result. 您执行“ OR因此可以想象只有一个ResultSet有结果。

What you end up with is trying to read from empty result sets. 您最终要尝试从空结果集中读取数据。

Suppose rs1 has one result and rs3 has 3 results. 假设rs1有一个结果,而rs3有3个结果。 Now as per your code it will fail for rs1.getString("type"); 现在按照您的代码,它将因rs1.getString("type");失败rs1.getString("type"); during second iteration. 在第二次迭代中。 Better to loop over each resultSet separately. 最好分别遍历每个resultSet。

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. 如果您的三个表之一缺少type值,这将导致严重错误。 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. 我会严重建议仅使用一个SQL语句,该语句将三个选择中的每一个都作为子选择,然后将它们全部连接在一起。 Your java can just iterate over the result from this one SQL statement. 您的Java可以仅迭代此SQL语句的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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