gpt4 book ai didi

java - 在结果集开始之前运行多个sql

转载 作者:行者123 更新时间:2023-11-30 02:52:49 25 4
gpt4 key购买 nike

我目前正在开发一家餐厅的应用程序。我当前正在处理的代码的想法是,它从组合框中获取名称,并将收据插入数据库中,其中包含正在处理该收据的人员的员工编号。我尝试执行的代码是:

    RestaurantOrder sqlRestaurantOrder = new RestaurantOrder();
ActionListener printReceiptListner;
printReceiptListner = (ActionEvent) -> {
int ID = Integer.parseInt(input1.getText());
try {
SystemManager manager = new SystemManager();
ResultSet rs = manager.stmt.executeQuery(sqlRestaurantOrder.setIdSql(ID));

while (rs.next()) {

double totalPrice = rs.getDouble("sumprice");
if (ID > 0) {
Receipt receiptSql = new Receipt();
String firstName = (String) cb.getSelectedItem();
String checkoutDate = new SimpleDateFormat("yyyyMMdd").format(Calendar.getInstance().getTime());
ResultSet rs2 = manager.stmt.executeQuery(receiptSql.getEmployeeId(firstName));
int employeeId = rs2.getInt("id");

while (rs2.next()) {

Receipt receiptSql2 = new Receipt();
ResultSet rs3 = manager.stmt.executeQuery(receiptSql2.SetStatusReceipt(employeeId, checkoutDate, ID, totalPrice));
while (rs3.next()) {
}
}

}
}

} catch (SQLException k) {
JOptionPane.showMessageDialog(null, k.getMessage());
}
};

声明如下:

public class Receipt {

public String sql;
public String sql2;

public String getEmployeeId(String firstName){
return sql2 = "SELECT id FROM employee WHERE firstName = '" + firstName + "';";
}

public String SetStatusReceipt(int employeeId, String checkoutDate, int id, double totalPrice){
return sql = "INSERT INTO `receipt` (`employeeId`, `price`, `restaurantOrderId`, `checkoutDate`) VALUES (" + employeeId + " , " + totalPrice + ", " + id + ", " + checkoutDate + ");";
};

}

我在结果集开始之前收到错误,我查了一下它的含义,但目前无法修复它。我们将不胜感激。

如果我忘记发布更重要的代码,请告诉我,我会更新它

最佳答案

您必须调用ResultSet.next()在您能够访问该 ResultSet 的字段之前。因此,您需要将 employeeId 的分配移至 while 循环中:

while (rs2.next()) {
int employeeId = rs2.getInt("id");

摘自ResultSet.next()的文档:

A ResultSet cursor is initially positioned before the first row; the first call to the method next makes the first row the current row; the second call makes the second row the current row, and so on.

关于java - 在结果集开始之前运行多个sql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38125041/

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