title: This code always give an error of duplicate insertion - How to solve that?
body: <p>I am trying to create an insertion in some tables with random data but i have a problem with the duplicates. </p>
  
  <p>I wrote an insertion of random records in postgresql b but it returns duplicates detection and there should be no duplicates i am using every name for exactly 2680 times and the names are 22 for 58960 records total of table play_in please check and tell what's wrong with it and how does produce a duplicate.</p>
  
  <p><code></code>import java.sql.Connection;
      import java.sql.DriverManager;
      import java.sql.Statement;
      import java.util.Random;</p>
  
  <pre><code>public class PostgreSQLJDBC {
      public static void main(String args[]) {
          Connection c = null;
          Statement stmt = null;
          int year;
          int year1;
          int num_rating;
          int num_ratingAvg;
          String[] round = new String[5];
          round[0] = "32nd";
          round[1] = "16th";
          round[2] = "quarter_final";
          round[3] = "SemiFinal";
          round[4] = "FInal";
          String[] name1 = new String[22];
          name1[0] = "ahmed";
          name1[1] = "mohamed";
          name1[2] = "mai";
          name1[3] = "salma";
          name1[4] = "walid";
          name1[5] = "wael";
          name1[6] = "fadwa";
          name1[7] = "nada";
          name1[8] = "nahla";
          name1[9] = "mustafa";
          name1[10] = "ola";
          name1[11] = "omar";
          name1[12] = "amr";
          name1[13] = "beshoy";
          name1[14] = "marina";
          name1[15] = "gerges";
          name1[16] = "botros";
          name1[17] = "mina";
          name1[18] = "menna";
          name1[19] = "feasal";
          name1[20] = "youssef";
          name1[21] = "moussa";
          Random id = new Random();
          Random roundc = new Random();
          String round1;
          int idc;
          String name;
          Random namec = new Random();
          int namecounter = 0;
          Random belle = new Random();
          int belec = 0;
          Random yearc = new Random();
          Random num_ratingsc = new Random();
          int num_ratingSum = 0;
          // roundc.nextInt(4);
          try {
              Class.forName("org.postgresql.Driver");
              c = DriverManager.getConnection(
                      "jdbc:postgresql://localhost:5432/a2", "mohamed", "1234");
              System.out.println("Opened database successfully");
              String sql3;
              String sql4;
              stmt = c.createStatement();
              String sql = "CREATE TABLE cup_matches "
                      + "(MID INT PRIMARY KEY     NOT NULL,"
                      + " ROUND           TEXT    NOT NULL, "
                      + " YEAR            INT     NOT NULL, "
                      + " NUM_RATINGS     INT     NOT NULL, "
                      + " RATING          INT     NOT NULL)";
              String sql1 = "CREATE TABLE played_in "
                      + "(MID         INT       NOT NULL,"
                      + " NAME           TEXT    NOT NULL, "
                      + " YEAR            INT     NOT NULL, "
                      + " POSITION     INT     NOT NULL, "
                      + "CONSTRAINT pk_played_in PRIMARY KEY (MID, NAME));";
  
              String sql2 = "ALTER TABLE played_in ADD CONSTRAINT ref FOREIGN KEY (MID) REFERENCES cup_matches (MID);";
  
              stmt.executeUpdate(sql);
              stmt.executeUpdate(sql1);
              stmt.executeUpdate(sql2);
  
              for (int i = 0; i &lt; 2680; i++) {
                  year = yearc.nextInt(2014 - 1900) + 1900;
                  num_rating = num_ratingsc.nextInt(1000);
                  num_ratingSum += num_rating;
                  num_ratingAvg = num_ratingSum / (i + 1);
                  round1 = "\'" + round[roundc.nextInt(4)] + "\'";
                  sql3 = "INSERT INTO cup_matches (MID, ROUND, YEAR, NUM_RATINGS, RATING) "
                          + "VALUES ("
                          + i
                          + ", "
                          + round1
                          + ", "
                          + year
                          + ", "
                          + num_rating + ", " + num_ratingAvg + ");";
                  stmt.executeUpdate(sql3);
                  System.out.println(sql3);
              }
              for (int j = 0; j &lt; 58960; j++) {
                  idc = j;
                  idc = (idc &gt;= 2679) ? 0 : idc;
                  year1 = yearc.nextInt(2014 - 1900) + 1900;
                  num_rating = num_ratingsc.nextInt(1000);
  
                  if ((belle.nextInt(2 - 1) + 1) == 1 &amp;&amp; belec &lt; 118) {
                      name = "\'" + name1[namecounter] + "belle" + "\'";
                      sql4 = "INSERT INTO played_in (MID, NAME, YEAR, POSITION) "
                              + "VALUES (" + idc + ", " + name + ", " + year1
                              + ", " + num_rating + ");";
                      stmt.executeUpdate(sql4);
  
                      belec++;
                      idc++;
                      System.out.println(idc + "              " + namecounter);
                      namecounter++;
                  } else {
                      name = "\'" + name1[namecounter] + "\'";
                      sql4 = "INSERT INTO played_in (MID, NAME, YEAR, POSITION) "
                              + "VALUES (" + idc + ", " + name + ", " + year1
                              + ", " + num_rating + ");";
                      stmt.executeUpdate(sql4);
                      namecounter++;
                      idc++;
                      System.out.println(idc + "              " + namecounter);
                  }
                  namecounter = (namecounter &lt; 22) ? namecounter : 0;
              }
              // stmt.executeUpdate(sql);
              // stmt.executeUpdate();
              // stmt.executeUpdate(sql2);
              stmt.close();
              c.close();
          } catch (Exception e) {
              System.err.println(e.getClass().getName() + ": " + e.getMessage());
              System.exit(0);
          }
          System.out.println("Table created successfully");
      }
  }
  </code></pre>