private void Button_SaveActionPerformed(
      java.awt.event.ActionEvent evt) { // GEN-FIRST:event_Button_SaveActionPerformed
    int tafel = 0;

    for (int a = 0; a < deelnemers.size(); a++) {
      OpstellingDeelnemer deelnemer = deelnemers.get(a);

      if (deelnemer.getTF_Code() != tafel) {
        String sql = "insert into opstelling (Ronde, Tafel, Toernooi) values (?,?,?)";
        try {
          Connection conn;
          conn = SimpleDataSourceV2.getConnection();
          PreparedStatement stat = conn.prepareStatement(sql);
          stat.setInt(1, deelnemer.getR_Code());
          stat.setInt(2, deelnemer.getTF_Code());
          stat.setInt(3, T_Code);
          stat.execute();

        } catch (Exception ex) {
          System.out.println(ex);
          JOptionPane.showMessageDialog(this, "Database Error" + ex.getMessage());
        }
        tafel++;
      }

      String sql = "insert into plaats (Ronde, Tafel, Toernooi, D_Code) values (?,?,?,?)";
      try {
        Connection conn;
        conn = SimpleDataSourceV2.getConnection();
        PreparedStatement stat = conn.prepareStatement(sql);
        stat.setInt(1, deelnemer.getR_Code());
        stat.setInt(2, deelnemer.getTF_Code());
        stat.setInt(3, T_Code);
        stat.setInt(4, deelnemer.getD_Code());
        stat.execute();

      } catch (Exception ex) {
        System.out.println(ex);
        JOptionPane.showMessageDialog(this, "Database Error" + ex.getMessage());
      }
    }
    new ToernooiStart(T_Code).setVisible(true);
    this.dispose();
  } // GEN-LAST:event_Button_SaveActionPerformed
  private void CreateOpstelling() {

    int aantal = 0;
    String sql =
        "select count(D_Code) from heeft_betaald where inleggeld_betaald = 'j' and T_Code = ?";

    if (R_Code != 0) {
      sql += " and D_Code in (select D_Code from plaats where Plaats = 1 and Ronde = ?)";
    }

    try {
      Connection conn;
      conn = SimpleDataSourceV2.getConnection();
      PreparedStatement stat = conn.prepareStatement(sql);

      stat.setInt(1, T_Code);

      if (R_Code != 0) {
        stat.setInt(2, R_Code);
      }

      ResultSet res = stat.executeQuery();

      while (res.next()) {
        aantal = res.getInt("count(D_Code)");
      }

    } catch (Exception ex) {
      System.out.println(ex);
      JOptionPane.showMessageDialog(this, "Database Error" + ex.getMessage());
    }

    if ((R_Code != 0) && (aantal < 3)) {
      includesecondplace = true;
      aantal += aantal;
    }

    int[] tavels = Opstelling_Generator.calculatetavels(aantal);
    System.out.println(aantal);
    int step = 0;
    int tavels_nr = 1;

    sql =
        "select voornaam, achternaam, D_Code from deelnemer where d_code in (select d_code from heeft_betaald where inleggeld_betaald = 'j' and t_code = ?)";

    if (R_Code != 0) {
      sql += " and D_Code in (select D_Code from plaats where Plaats = 1 ";
      if (includesecondplace) {
        sql += "or Plaats = 2 and Ronde = ?)";
      } else {
        sql += "and Ronde = ?)";
      }
    }

    try {
      Connection conn;
      conn = SimpleDataSourceV2.getConnection();
      PreparedStatement stat = conn.prepareStatement(sql);

      stat.setInt(1, T_Code);

      if (R_Code != 0) {
        stat.setInt(2, R_Code);
      }

      ResultSet res = stat.executeQuery();

      while (res.next()) {
        String voornaam = res.getString("voornaam");
        String achternaam = res.getString("achternaam");
        int d_code = res.getInt("D_Code");

        int max_table = 8;

        if (tavels[7] != 0) {
          max_table = 8;
        } else if (tavels[6] != 0) {
          max_table = 7;
        } else if (tavels[5] != 0) {
          max_table = 6;
        } else if (tavels[4] != 0) {
          max_table = 5;
        } else if (tavels[3] != 0) {
          max_table = 4;
        } else if (tavels[2] != 0) {
          max_table = 3;
        } else if (tavels[1] != 0) {
          max_table = 2;
        } else if (tavels[0] != 0) {
          max_table = 1;
        }

        if (step == 0) {
          step = max_table;
        }

        step--;

        OpstellingDeelnemer op =
            new OpstellingDeelnemer(R_Code + 1, tavels_nr, d_code, voornaam, achternaam, 0);
        deelnemers.add(op);

        if (step == 0) {
          tavels[max_table]--;
          tavels_nr++;
        }
      }

    } catch (Exception ex) {
      System.out.println(ex);
      JOptionPane.showMessageDialog(this, "Database Error" + ex.getMessage());
    }

    for (int a = 0; a < deelnemers.size(); a++) {
      OpstellingDeelnemer deelnemer = deelnemers.get(a);
      model.addRow(deelnemer.torow());
    }
  }