结果集开始前 运行 多个 sql

Before start of result set running multiple sql

我目前正在处理一家餐馆的申请。我目前正在处理的代码的想法是,它从组合框中获取名称,并将收据插入到数据库中,其中包含正在处理它的人员的员工编号。我正在尝试执行的代码是:

    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 + ");";
    };

}

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

如果我忘了 post 更重要的代码让我知道我会更新它

您必须调用 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.