2014-01-25 54 views
0

我得到了这个错误:java.sql.SQLException: Illegal operation on empty result set.我不明白为什么,我循环搜索最终搜索到的行。非法结果集

rs1=st1.executeQuery("select product.price,product.id from product,warehouse,shop 
           where product.brand ='" 
           + Brand 
           + "' and product.productType='" 
           + Product 
           + "'and shop.quantity + warehouse.quantity='" 
           + Quantity 
           + "' and product.id=shop.idProduct"); 
       while (rs1.next()) { 
        price = rs.getInt("price"); 
        idProduct = rs.getString("id"); 
        countWareHouse++; 
       } 
       if (countWareHouse > 0) { 
        JOptionPane.showConfirmDialog(Sale, 
          "Do you want to add this " + Product 
            + "int the cart?", "CART", 
          JOptionPane.OK_CANCEL_OPTION); 
        Prices.add(price); 
       } 
       } 
+0

你能显示完整的代码吗? –

+1

你没有向我们展示这是罪魁祸首的代码。 “while”循环中有什么。并使用'PreparedStatement'来构建查询。 –

+0

“品牌”,“产品”和“数量”应该是避免SQL注入攻击的参数。 – dasblinkenlight

回答

3

在循环中,您使用的是rs而不是rs1。如果这不是发布编译器错误,我假设rs位于全局名称空间中,但在当前状态为空。

+0

你发现它。 –

+0

我还没有看到这个,四眼比一个更好,谢谢 – OiRc

+0

推荐:在ResultSet实例上使用文字名称,而不是“rs1”等。更好:productPriceResultSet e.a.为了阻止。 –