Advertisement
Guest User

Untitled

a guest
May 8th, 2018
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.28 KB | None | 0 0
  1. import com.github.javafaker.Faker;
  2.  
  3. import java.sql.*;
  4. import java.util.*;
  5. import java.util.Date;
  6.  
  7. public class StatistikaDBS {
  8.  
  9.     public static void main(String[] args) {
  10.         Connection c = null;
  11.         PreparedStatement pst = null;
  12.         Statement st = null;
  13.         Faker faker = new Faker();
  14.         try {
  15.             Class.forName("org.postgresql.Driver");
  16.             c = DriverManager
  17.                     .getConnection("jdbc:postgresql://localhost:5432/dbs2018",
  18.                             "postgres", "chameleon");
  19.             c.setAutoCommit(false);
  20.             System.out.println("Opened database successfully");
  21.  
  22.  
  23.             Random rand = new Random();
  24.  
  25.             pst = c.prepareStatement("INSERT INTO statistika (stat_id,pocet_golov,pocet_min,asistencie,vyluceny,zapas_id,hrac_id) VALUES(?,?,?,?,?,?,?)");
  26.  
  27.             int celkovyPocet = 99992;
  28.  
  29.             for (int zapasID = 4001; zapasID <= 4010; zapasID++) {
  30.                 String query = "SELECT team1_id, team2_id " + " FROM " + " zapas " + "WHERE zapas_id=" + zapasID;
  31.                 st = c.createStatement();
  32.                 ResultSet rs = st.executeQuery(query);
  33.  
  34.  
  35.                 int tim1 = 1;
  36.                 int tim2 = 1;
  37.                 while (rs.next()) {
  38.                     tim1 = rs.getInt("team1_id");
  39.                     tim2 = rs.getInt("team2_id");
  40.                 }
  41.  
  42.                 //toto sprav 2x
  43.                 int timID = 0;
  44.                 for (int t = 1; t <= 2; t++) {
  45.                     if (t == 1)
  46.                         timID = tim1;
  47.                     else timID = tim2;
  48.  
  49.                     query = "SELECT hrac_id" + " FROM " + " hrac  " + " WHERE team_id =" + timID;
  50.                     st = c.createStatement();
  51.                     rs = st.executeQuery(query);
  52.                     List<Integer> hraci = new ArrayList<Integer>();
  53.                     int hrac_id = 0;
  54.                     while (rs.next()) {
  55.                         hrac_id = rs.getInt("hrac_id");
  56.                         hraci.add(hrac_id);
  57.                     }
  58.  
  59.                     //TU SA DEJU ZAZRAKY
  60.                     int pocet_hracov = faker.random().nextInt(11, 14);
  61.                     List<Integer> hraciNaVyber = new LinkedList<Integer>();
  62.  
  63.                     for (int i = 0; i < 20; i++)  //20 hracov
  64.                         hraciNaVyber.add(i + 1);
  65.  
  66.  
  67.                     int pocetGolov = 0;
  68.  
  69.                     int vylucenie = rand.nextInt(2);
  70.  
  71.                     int casLimit;
  72.                     if (vylucenie == 0)
  73.                         casLimit = (pocet_hracov - (((pocet_hracov - 10) * 2) - 2)) * 90;
  74.                     else
  75.                         casLimit = (pocet_hracov - (((pocet_hracov - 10) * 2) - 1)) * 90;
  76.  
  77.                     int pocetMin = 0;
  78.  
  79.                     int aktMin = 0;
  80.  
  81.                     int uzVyluceny = 0;
  82.  
  83.                     int aktpocetGolov = 0;
  84.  
  85.                     for (int h = 1; h <= pocet_hracov; h++) {
  86.                         int indexVyberu = rand.nextInt(hraciNaVyber.size());
  87.                         int vybranyHrac = hraciNaVyber.get(indexVyberu);
  88.                         hraciNaVyber.remove(indexVyberu);
  89.  
  90.                         pst.setInt(1, celkovyPocet);
  91.                         int pravdepodobnost = faker.random().nextInt(1, 100);
  92.                         if (pravdepodobnost >= 80)
  93.                             aktpocetGolov = 1;
  94.                         else
  95.                             aktpocetGolov = 0;
  96.  
  97.                         pst.setInt(2, aktpocetGolov);
  98.  
  99.                         int asistencia = 0;
  100.                         if (pocetGolov > 0) {
  101.                             asistencia = 1;
  102.                             pocetGolov--;
  103.                         }
  104.                         pocetGolov += aktpocetGolov;
  105.  
  106.                         pst.setInt(4, asistencia);
  107.  
  108.  
  109.                         if (pocetMin >= casLimit) {
  110.                             pst.setInt(3, 45);
  111.                             aktMin = 45;
  112.                         } else {
  113.                             aktMin = 90;
  114.                             pst.setInt(3, 90);
  115.                             pocetMin += 90;
  116.                         }
  117.  
  118.                         if (vylucenie == 1 && aktMin == 45 && uzVyluceny == 0) {
  119.                             uzVyluceny = 1;
  120.                             pst.setBoolean(5, true);
  121.                         } else
  122.                             pst.setBoolean(5, false);
  123.  
  124.  
  125.                         pst.setInt(6, zapasID);
  126.                         pst.setInt(7, hraci.get(vybranyHrac - 1));
  127.  
  128.                         if (celkovyPocet == 3000 || celkovyPocet == 4000 || celkovyPocet == 5000)
  129.                             System.out.println("Zaznam cislo " + celkovyPocet);
  130.                         celkovyPocet++;
  131.                         //pst.addBatch();
  132.                         pst.executeUpdate();
  133.                     }
  134.                 }
  135.                 //System.out.println("Executol som");
  136.                // pst.executeBatch();
  137.  
  138.             }
  139.  
  140.             /*for (int i = 1; i <= 1000000; i++) {
  141.                 int pocet = rand.nextInt(5)+1;
  142.                 for(int j=1; j<=pocet;j++) {
  143.  
  144.                     long datum_nar = faker.date().birthday(18,90).getTime();
  145.                     Date d = new Date(datum_nar);
  146.                     Date actD = new Date();
  147.                     long datum_reg = faker.date().between(d,actD).getTime();
  148.  
  149.                     pst.setString(1, faker.name().firstName());
  150.                     pst.setString(2, faker.name().lastName());
  151.                     pst.setDate(3, new java.sql.Date(datum_nar));
  152.                     pst.setString(4, faker.address().city());
  153.                     pst.setDate(5, new java.sql.Date(datum_reg));
  154.                     pst.setInt(6, celkovyPocet);
  155.                     pst.setInt(7, i);
  156.                     pst.executeUpdate();
  157.                     celkovyPocet++;
  158.                 }
  159.             }*/
  160.  
  161.  
  162.             pst.close();
  163.             st.close();
  164.             c.commit();
  165.             c.close();
  166.  
  167.         } catch (Exception e) {
  168.             e.printStackTrace();
  169.             System.err.println(e.getClass().getName() + ": " + e.getMessage());
  170.             System.exit(0);
  171.         }
  172.         System.out.println("Records created successfully");
  173.     }
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement