title: Exception in thread "main" java.lang.RuntimeException: org.postgresql.util.PSQLException: FATAL: sorry, too many clients already
body: <p>I have this exception in my code, so :</p>
  
  <p>Here is the code where I have the exception : </p>
  
  <pre><code>private void assignCoursesToStudents() throws SQLException, ClassNotFoundException, IOException {
  
      ConnectionToDatabase connectionToDatabase = new ConnectionToDatabase();
  
  Random random = new Random();
  
  List&lt;Student&gt; students = generateStudents();
      for (int i = 0; i &lt; students.size(); i++) {
      int randomNumberOfStudentsToOneCourse = random.nextInt(3) + 1;
  
      Set&lt;Integer&gt; randomInts = new HashSet();
      while (randomInts.size() &lt; randomNumberOfStudentsToOneCourse) {
          randomInts.add(random.nextInt(10) + 1);
      }
      for (int j = 0; j &lt; randomInts.size(); j++) {
          insertStudentsToCoursesToDatabase(i, j, randomInts,connectionToDatabase);
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>insertStudentsToCoursesToDatabase method : </p>
  
  <pre><code> private void insertStudentsToCoursesToDatabase(int i, int j, Set&lt;Integer&gt; randomInts, ConnectionToDatabase connectionToDatabase) throws IOException, ClassNotFoundException, SQLException {
  
  
  
      try {
          try (PreparedStatement preparedStatement = connectionToDatabase.connectToDatabase()
                  .prepareStatement("INSERT INTO students_courses (student_id, course_id) VALUES(?, ?)")) {
              preparedStatement.setInt(1, i + 1);
              preparedStatement.setInt(2, (int) randomInts.toArray()[j]);
              preparedStatement.executeUpdate();
          }
      } catch (SQLException ex) {
          throw new RuntimeException(ex);
  // HERE I HAVE AN EXCEPTION!
  
      }
  }
  </code></pre>
  
  <p>Also I have the class where I connect to Database : </p>
  
  <pre><code>public class ConnectionToDatabase {
  
  public Connection connectToDatabase() throws IOException, ClassNotFoundException, SQLException {
  
      BufferedReader bufferedReader = new BufferedReader(new FileReader("C:\\db.properties"));
      Properties properties=new Properties ();
      properties.load (bufferedReader);
      String driverName= (String) properties.get ("jdbc.sql.driver");
  
      String url= (String) properties.get ("jdbc.sql.url");
      String username = (String) properties.get ("jdbc.sql.username");
      String password= (String) properties.get ("jdbc.sql.password");
      Class.forName(driverName);
  
      Connection connection = DriverManager.getConnection(
              url, username, password);
  
  
      return connection;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>And my exception : </p>
  
  <pre><code>Exception in thread "main" java.lang.RuntimeException: org.postgresql.util.PSQLException: FATAL: sorry, too many clients already
  at com.foxminded.sql.util.GeneratedData.insertStudentsToCoursesToDatabase(GeneratedData.java:58)
  at com.foxminded.sql.util.GeneratedData.assignCoursesToStudents(GeneratedData.java:41)
  at com.foxminded.sql.util.GeneratedData.generateTestData(GeneratedData.java:23)
  at com.foxminded.sql.dao.SchoolDaoImplementation.initializeTheDatabase(SchoolDaoImplementation.java:360)
  at com.foxminded.sql.Main.main(Main.java:16)
  </code></pre>
  
  <p>When I am trying to run code without class "ConnectionToDatabase" and make connection directly in the class I need, I didn't have any errors.</p>