gpt4 book ai didi

java - 对空结果集的非法操作

转载 作者:行者123 更新时间:2023-11-29 03:36:43 24 4
gpt4 key购买 nike

<分区>

我正尝试在一家杂货店中构建一个收费台,我的代码实际上执行了我希望它执行的操作,但仅此而已。

在我要求用户输入他们想要的商品数量后,产品信息被收集并且工作正常,但是当它应该要求用户输入下一个产品的产品 ID 时,该行重复并且我在我的捕获中得到以下异常:“对空结果集的非法操作”。同样,除了重复该行之外,所有计算和一切都很好。关于可能是什么问题的任何想法?

重复的输出是这样的:

Enter product (or Exit):

ERROR1: Illegal operation on empty result set.

Enter product (or Exit):

这是代码。

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());
}

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com