Advertisement
Guest User

Untitled

a guest
Jan 13th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.69 KB | None | 0 0
  1.     private static void addUsers(Connection connection, int count, int minLength, int maxLength) throws SQLException {
  2.         Statement statement = connection.createStatement();
  3.  
  4.         usersIDList = new ArrayList<Integer>(count);
  5.  
  6.         for (int index = 0; index < count; index++) {
  7.             final String username = RandomStringUtils.randomAlphabetic(new Random().nextInt(maxLength - minLength) + minLength) + index;
  8.             final String password = RandomStringUtils.randomAlphabetic(20);
  9.             final String info = RandomStringUtils.randomAlphabetic(20);
  10.             final String pathpic = RandomStringUtils.randomAlphabetic(20);
  11.             statement.executeUpdate(String.format("INSERT INTO users (login, password, info, path_pic) VALUES (\'%s\',\'%s\',\'%s\',\'%s\');", username, password, info, pathpic));
  12.             ResultSet res = statement.executeQuery("SELECT lastval();");
  13.             res.next();
  14.             int userID = res.getInt(1);
  15.             usersIDList.add(userID);
  16.         }
  17.  
  18.         statement.close();
  19.     }
  20.  
  21.     private static void addSubscriptions(Connection connection, int minCount, int maxCount) throws SQLException {
  22.         Statement statement = connection.createStatement();
  23.  
  24.         for (int id : usersIDList) {
  25.             final int count = new Random().nextInt(maxCount - minCount) + minCount + 1;
  26.             for (int i = 0; i < count; i++) {
  27.                 if (id == usersIDList.get(i)) {
  28.                     continue;
  29.                 }
  30.                 statement.executeUpdate(String.format("INSERT INTO subscriptions (subscriber_id, to_user_id) VALUES (%d, %d);", id, usersIDList.get(i)));
  31.             }
  32.         }
  33.  
  34.         statement.close();
  35.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement