Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import com.github.javafaker.Faker;
- import java.sql.*;
- import java.util.*;
- import java.util.Date;
- public class StatistikaDBS {
- public static void main(String[] args) {
- Connection c = null;
- PreparedStatement pst = null;
- Statement st = null;
- Faker faker = new Faker();
- try {
- Class.forName("org.postgresql.Driver");
- c = DriverManager
- .getConnection("jdbc:postgresql://localhost:5432/dbs2018",
- "postgres", "chameleon");
- c.setAutoCommit(false);
- System.out.println("Opened database successfully");
- Random rand = new Random();
- pst = c.prepareStatement("INSERT INTO statistika (stat_id,pocet_golov,pocet_min,asistencie,vyluceny,zapas_id,hrac_id) VALUES(?,?,?,?,?,?,?)");
- int celkovyPocet = 99992;
- for (int zapasID = 4001; zapasID <= 4010; zapasID++) {
- String query = "SELECT team1_id, team2_id " + " FROM " + " zapas " + "WHERE zapas_id=" + zapasID;
- st = c.createStatement();
- ResultSet rs = st.executeQuery(query);
- int tim1 = 1;
- int tim2 = 1;
- while (rs.next()) {
- tim1 = rs.getInt("team1_id");
- tim2 = rs.getInt("team2_id");
- }
- //toto sprav 2x
- int timID = 0;
- for (int t = 1; t <= 2; t++) {
- if (t == 1)
- timID = tim1;
- else timID = tim2;
- query = "SELECT hrac_id" + " FROM " + " hrac " + " WHERE team_id =" + timID;
- st = c.createStatement();
- rs = st.executeQuery(query);
- List<Integer> hraci = new ArrayList<Integer>();
- int hrac_id = 0;
- while (rs.next()) {
- hrac_id = rs.getInt("hrac_id");
- hraci.add(hrac_id);
- }
- //TU SA DEJU ZAZRAKY
- int pocet_hracov = faker.random().nextInt(11, 14);
- List<Integer> hraciNaVyber = new LinkedList<Integer>();
- for (int i = 0; i < 20; i++) //20 hracov
- hraciNaVyber.add(i + 1);
- int pocetGolov = 0;
- int vylucenie = rand.nextInt(2);
- int casLimit;
- if (vylucenie == 0)
- casLimit = (pocet_hracov - (((pocet_hracov - 10) * 2) - 2)) * 90;
- else
- casLimit = (pocet_hracov - (((pocet_hracov - 10) * 2) - 1)) * 90;
- int pocetMin = 0;
- int aktMin = 0;
- int uzVyluceny = 0;
- int aktpocetGolov = 0;
- for (int h = 1; h <= pocet_hracov; h++) {
- int indexVyberu = rand.nextInt(hraciNaVyber.size());
- int vybranyHrac = hraciNaVyber.get(indexVyberu);
- hraciNaVyber.remove(indexVyberu);
- pst.setInt(1, celkovyPocet);
- int pravdepodobnost = faker.random().nextInt(1, 100);
- if (pravdepodobnost >= 80)
- aktpocetGolov = 1;
- else
- aktpocetGolov = 0;
- pst.setInt(2, aktpocetGolov);
- int asistencia = 0;
- if (pocetGolov > 0) {
- asistencia = 1;
- pocetGolov--;
- }
- pocetGolov += aktpocetGolov;
- pst.setInt(4, asistencia);
- if (pocetMin >= casLimit) {
- pst.setInt(3, 45);
- aktMin = 45;
- } else {
- aktMin = 90;
- pst.setInt(3, 90);
- pocetMin += 90;
- }
- if (vylucenie == 1 && aktMin == 45 && uzVyluceny == 0) {
- uzVyluceny = 1;
- pst.setBoolean(5, true);
- } else
- pst.setBoolean(5, false);
- pst.setInt(6, zapasID);
- pst.setInt(7, hraci.get(vybranyHrac - 1));
- if (celkovyPocet == 3000 || celkovyPocet == 4000 || celkovyPocet == 5000)
- System.out.println("Zaznam cislo " + celkovyPocet);
- celkovyPocet++;
- //pst.addBatch();
- pst.executeUpdate();
- }
- }
- //System.out.println("Executol som");
- // pst.executeBatch();
- }
- /*for (int i = 1; i <= 1000000; i++) {
- int pocet = rand.nextInt(5)+1;
- for(int j=1; j<=pocet;j++) {
- long datum_nar = faker.date().birthday(18,90).getTime();
- Date d = new Date(datum_nar);
- Date actD = new Date();
- long datum_reg = faker.date().between(d,actD).getTime();
- pst.setString(1, faker.name().firstName());
- pst.setString(2, faker.name().lastName());
- pst.setDate(3, new java.sql.Date(datum_nar));
- pst.setString(4, faker.address().city());
- pst.setDate(5, new java.sql.Date(datum_reg));
- pst.setInt(6, celkovyPocet);
- pst.setInt(7, i);
- pst.executeUpdate();
- celkovyPocet++;
- }
- }*/
- pst.close();
- st.close();
- c.commit();
- c.close();
- } catch (Exception e) {
- e.printStackTrace();
- System.err.println(e.getClass().getName() + ": " + e.getMessage());
- System.exit(0);
- }
- System.out.println("Records created successfully");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement