Search code examples
javasqlresultset

Before start of result set running multiple sql


I'm currently working on an application for a restaurant. The idea for the code I'm currently working on is that it gets the name from a combobox and inserts a receipt into the database whith the employee number of the person who is working on it. The code whith which I'm trying to do it is:

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

The statements are:

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

}

I'm getting the error before start of result set which I looked up what it means but I can't get it fixed at the moment. Help will be appreciated.

If I forgot to post more important code let me know I'll update it


Solution

  • You have to call ResultSet.next() before you are able to access the fields of that ResultSet. So you need to move your assignment of employeeId into your while loop:

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

    From the documentation of 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.