2014-01-25 55 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.爲了阻止。 –