对空结果集的非法操作 [英] Illegal operation on empty result set

查看:182
本文介绍了对空结果集的非法操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一家杂货店里建立一个付费台,我的代码实际上是为了完成我打算做的工作,但有一件事。

I'm trying to build a pay desk in a grocery store and my code actually performs what I intend for it to do, but for one thing.

之后我要求用户输入他们想要的项目数量,产品信息被收集并且工作正常,但是当应该要求用户输入下一个产品的产品ID时,该行被重复,并且得到以下异常在我的抓住:空结果集合的非法操作。再次,所有的计算和一切都很好,除了重复这一行。任何想法可能是什么问题?

After I ask the user to enter how many of an item they want, the product information is gathered and works fine, but when it's supposed to ask the user to enter product-ID for next product, the line is repeated and I get the following exception in my catch: "Illegal operation on empty result set". Again, all the calculations and everything is fine except for the repeating of that line. Any ideas on what might be the problem?

重复的输出是这样的:


输入产品(或退出):

Enter product (or Exit):

ERROR1:空结果集上的非法操作。

ERROR1: Illegal operation on empty result set.

输入产品(或退出):

这里是代码。

try {
  Class.forName("com.mysql.jdbc.Driver");
  String connection = "jdbc:mysql://myDB?";
  connection = connection + "user=xxx&password=xxxxxx";
  Connection conn = DriverManager.getConnection(connection);
  // MATA IN PRODUKTNUMMER
  System.out.println("\nEnter product (or Exit):");
  GroceryStore.input = GroceryStore.scan.nextLine();

  PreparedStatement stmt = conn.prepareStatement(
          "SELECT * "+
          "FROM Products "+
          "WHERE productNo = ?");
          stmt.setString(1, GroceryStore.input); 

          ResultSet rs = stmt.executeQuery();
          rs.next();

    pName = rs.getString("productName");
    System.out.println("Product: " + pName);

    // MATA IN ANTAL
    System.out.println("\nEnter amount:");
    GroceryStore.amount = GroceryStore.scan.nextInt();


    pPrice = rs.getDouble("productPrice");
    priceRounded = new BigDecimal(pPrice).setScale(2, BigDecimal.ROUND_FLOOR);
    amountRounded = new BigDecimal(GroceryStore.amount).setScale(0);
    priceRounded = priceRounded.multiply(amountRounded);
    GroceryStore.sum = GroceryStore.sum.add(priceRounded);

    inOut.output();  
    inOut.input();
    conn.close();
    }
    catch (Exception e) {
         System.out.println("ERROR1: " + e.getMessage());
    }


推荐答案

您尚未检查您的结果集在实际从Result集中重新获取值之前为空或不存在...

You haven't checked Whether your result set is empty or not before actually retriving values from Result set...

next()如果结果集中有数据,则返回true,否则为false不是在光标位置的数据
放置你的代码这样

next() returns true if there is a data in the result set, false it there is not data at the a cursor position Place your code like this

While(rs.next())
{
    pName = rs.getString("productName");
    System.out.println("Product: " + pName);

    // MATA IN ANTAL
    System.out.println("\nEnter amount:");
    GroceryStore.amount = GroceryStore.scan.nextInt();


    pPrice = rs.getDouble("productPrice");
}

这篇关于对空结果集的非法操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆