title: Execute java sql statements in sequence before another statements
body: <p>The error:</p>
  
  <pre><code>org.postgresql.util.PSQLException: ERROR: insert or update on table "food_order" violates foreign key constraint "food_order_invoice_id_fkey"
    Detail: Key (invoice_id)=(2) is not present in table "invoice".
  </code></pre>
  
  <p>First I insert my data to invoice table and then insert the id of that as reference for another. I want these statements executed sequentially.</p>
  
  <p>This is my code:</p>
  
  <pre><code>public static void insertInvoice(Invoice invoice) throws SQLException {
      Connection connection = DatabaseConnectionPostgre.connection();
      Statement statement = connection.createStatement();
      PaymentType paymentType = invoice.getPaymentType();
      String query = null;
      switch (paymentType){
          case Cash:
              query = "insert into cash_invoice values(" +
                      "'" + invoice.getId() + "', " +
                      "'" + invoice.getDate() + "', " +
                      "'" + invoice.getTotalPrice() + "', " +
                      "'" + invoice.getCustomer().getId() + "', " +
                      "'" + invoice.getInvoiceStatus() + "', " +
                      "'" + ((CashInvoice) invoice).getDeliveryFee() + "' " +
                      ")";
              break;
          case Cashless:
              query = "insert into cash_invoice values(" +
                      "'" + invoice.getId() + "', " +
                      "'" + invoice.getDate() + "', " +
                      "'" + invoice.getTotalPrice() + "', " +
                      "'" + invoice.getCustomer().getId() + "', " +
                      "'" + invoice.getInvoiceStatus() + "', " +
                      "'" + ((CashlessInvoice) invoice).getPromo() + "' " +
                      ")";
              break;
      }
      statement.executeUpdate(query);
      for (Food food:
           invoice.getFoods()) {
          query = "insert into food_order( food_id, invoice_id) values(" +
                  "'" + food.getId() + "', " +
                  "'" + invoice.getId() + "' " +
                  ")";
          statement.execute(query);
      }
      statement.close();
      connection.close();
  }
  </code></pre>