Guest User

Untitled

a guest
Dec 17th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.68 KB | None | 0 0
  1. import java.sql.*;
  2. import java.text.DateFormat;
  3. import java.text.DecimalFormat;
  4. import java.text.SimpleDateFormat;
  5. import java.time.ZoneId;
  6. import java.time.ZonedDateTime;
  7. import java.time.temporal.ChronoUnit;
  8. import java.util.Arrays;
  9. import java.util.List;
  10. import java.util.Random;
  11.  
  12. public class Generator {
  13.  
  14. private static final String DB_DRIVER = "oracle.jdbc.driver.OracleDriver";
  15. private static final String DB_CONNECTION = "jdbc:oracle:thin:@localhost:1521:orcl";
  16. private static final String DB_USER = "admin";
  17. private static final String DB_PASSWORD = "admin";
  18. private static final DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
  19. /////////////////////////////////
  20. private static final List<String> cities = Arrays.asList("Jefferson Hills", "Looney", "Daphna", "Pohick Park",
  21. "Berkley", "Eldoradania", "Cape Carlyn", "Winterpock", "Buffalo Forge", "Lindau Woods", "Tye River",
  22. "Maces Spring", "Mount Blanco", "Penhook", "Sleepy Hollow Estates", "Carrol Mill", "Clarkton", "Ocala",
  23. "Athens", "Oaklands", "East Courthouse Estates", "Oak Terrace", "Fox", "Patna", "Rocky Gap", "Cedar Forest",
  24. "Monaskon", "Spivey Mill", "Hinnom", "Reynolds Corner", "Cedar Green", "Todds Tavern", "Carrsville",
  25. "Rock Run", "Colony", "Putneys Mill", "Shiloh", "Allison Gap", "Prospect", "County Line Cross Roads");
  26.  
  27. private static final List<String> streets = Arrays.asList("Clear Hollow", "Quaking Willow Green", "Sleepy Chase",
  28. "Dusty Sky Turnabout", "Hidden Square", "Fallen Branch Place", "Thunder Gate Port", "Indian Hickory Mount",
  29. "Foggy Apple Arbor", "High Treasure Close", "Rustic Towers", "Emerald Elk Landing", "Cozy Rise Pathway",
  30. "Cotton Avenue", "Wishing Wagon Byway", "Silent Carrefour", "Shady Walk", "Velvet Edge", "Old Point",
  31. "Colonial Dell", "Red Loop", "Quiet Round", "Tawny Glade", "Middle Thicket", "Hazy Log Highlands",
  32. "Lazy Trail", "Heather Oak Dale", "Grand Prairie Knoll", "Crystal Rise", "Umber Maze", "Jagged Crescent",
  33. "Green Blossom Plaza", "Lost Forest Subdivision", "Bright Anchor Estates", "Gentle Autumn Vale",
  34. "Harvest Pond Route", "Easy Timber Orchard", "Golden Deer Manor", "Burning Hills Link", "Sunny Vista",
  35. "Stony Corners", "Honey Bend", "Noble Fox Bank", "Cinder Spring Circle", "Blue Embers Jetty", "Amber Ramp",
  36. "Little Crossing", "Misty Heights", "Pleasant Pioneer Boulevard", "Rocky Highway");
  37. private static final List<String> postalCodes = Arrays.asList("1");
  38. private static final List<String> genders = Arrays.asList("f", "m");
  39. private static final List<String> maritalStatus = Arrays.asList("single", "married");
  40. private static final List<String> position = Arrays.asList("junior", "regular", "senior");
  41.  
  42. private static Connection getDBConnection() {
  43.  
  44. Connection dbConnection = null;
  45.  
  46. try {
  47.  
  48. Class.forName(DB_DRIVER);
  49.  
  50. } catch (ClassNotFoundException e) {
  51.  
  52. System.out.println(e.getMessage());
  53.  
  54. }
  55.  
  56. try {
  57.  
  58. dbConnection = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD);
  59. return dbConnection;
  60.  
  61. } catch (SQLException e) {
  62.  
  63. System.out.println(e.getMessage());
  64.  
  65. }
  66.  
  67. return dbConnection;
  68.  
  69. }
  70.  
  71.  
  72. public static void fillWithDataBiuro() throws SQLException {
  73. Connection dbConnection = null;
  74. PreparedStatement preparedStatement = null;
  75. Random rand = new Random();
  76.  
  77.  
  78. try {
  79. dbConnection = getDBConnection();
  80. String insertTableSQL = "INSERT INTO BIURO(idbiura, miasto, ulica, kodpocztowy) VALUES (?,?,?,?)";
  81. preparedStatement = dbConnection.prepareStatement(insertTableSQL);
  82.  
  83. for (int i = 0; i < 1500000; i++) {
  84. System.out.println(i);
  85. preparedStatement.setInt(1, i);
  86. preparedStatement.setString(2, cities.get(rand.nextInt(cities.size())));
  87. preparedStatement.setString(3, streets.get(rand.nextInt(streets.size())));
  88. preparedStatement.setString(4, "22-400");
  89. preparedStatement.addBatch();
  90. //preparedStatement.execute();
  91. //statement.executeUpdate(insertTableSQL);
  92. //dbConnection.commit();
  93. }
  94.  
  95. preparedStatement.executeBatch();
  96. dbConnection.commit();
  97. System.out.println("Record is inserted into Biuro table!");
  98.  
  99.  
  100. } catch (SQLException e) {
  101.  
  102. System.out.println(e.getMessage());
  103.  
  104. } finally {
  105.  
  106. if (preparedStatement != null) {
  107. preparedStatement.close();
  108. }
  109.  
  110. if (dbConnection != null) {
  111. dbConnection.close();
  112. }
  113.  
  114. }
  115. System.out.println("STOP BIURO");
  116. }
  117.  
  118. public static void fillWithDataKlient() throws SQLException {
  119. Connection dbConnection = null;
  120. PreparedStatement preparedStatement = null;
  121. Random rand = new Random();
  122.  
  123.  
  124. try {
  125. dbConnection = getDBConnection();
  126. String insertTableSQL = "INSERT INTO KLIENT(idklienta, imie, nazwisko, miasto, ulica, telefon, wiek, stancywilny, plec) VALUES (?,?,?,?,?,?,?,?,?)";
  127. preparedStatement = dbConnection.prepareStatement(insertTableSQL);
  128.  
  129. for (int i = 0; i < 1500000; i++) {
  130. System.out.println(i);
  131. preparedStatement.setInt(1, i);
  132. preparedStatement.setString(2, "X");
  133. preparedStatement.setString(3, "Y");
  134. preparedStatement.setString(4, cities.get(rand.nextInt(cities.size())));
  135. preparedStatement.setString(5, streets.get(rand.nextInt(streets.size())));
  136. preparedStatement.setString(6, "22-400");
  137. preparedStatement.setInt(7, rand.nextInt((80 - 18) + 1) + 18);
  138. preparedStatement.setString(8, maritalStatus.get(rand.nextInt(maritalStatus.size())));
  139. preparedStatement.setString(9, genders.get(rand.nextInt(genders.size())));
  140. preparedStatement.addBatch();
  141. //preparedStatement.execute();
  142. //statement.executeUpdate(insertTableSQL);
  143. //dbConnection.commit();
  144. }
  145.  
  146. preparedStatement.executeBatch();
  147. dbConnection.commit();
  148. System.out.println("Record is inserted into Klient table!");
  149.  
  150.  
  151. } catch (SQLException e) {
  152.  
  153. System.out.println(e.getMessage());
  154.  
  155. } finally {
  156.  
  157. if (preparedStatement != null) {
  158. preparedStatement.close();
  159. }
  160.  
  161. if (dbConnection != null) {
  162. dbConnection.close();
  163. }
  164.  
  165. }
  166. System.out.println("STOP Klient");
  167.  
  168.  
  169. }
  170.  
  171. public static void fillWithDataNieruchomosc() throws SQLException {
  172.  
  173. Connection dbConnection = null;
  174. PreparedStatement preparedStatement = null;
  175. Random rand = new Random();
  176.  
  177.  
  178. try {
  179. dbConnection = getDBConnection();
  180. String insertTableSQL = "INSERT INTO nieruchomosc(idnieruchomosci, miasto, ulica, liczbapokoi, kodpocztowy, powierzchnia,odcentrum, rokbudowy, pietro, piwnica, balkon, wlasciciel_idwlasciciela,pracownik_idpracownika) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)";
  181. preparedStatement = dbConnection.prepareStatement(insertTableSQL);
  182.  
  183. for (int i = 0; i < 1000000; i++) {
  184. System.out.println(i);
  185. int surface = rand.nextInt((150 - 25) + 1) + 25;
  186. int numbersOfRoom;
  187. if(surface>=25 && surface <60){
  188. numbersOfRoom = rand.nextInt((4 - 1) + 1) + 1;
  189. } else if (surface >= 60 && surface <= 90){
  190. numbersOfRoom = rand.nextInt((6 - 3) + 1) + 3;
  191. } else if(surface >90 && surface <150){
  192. numbersOfRoom = rand.nextInt((8 - 5) + 1) + 5;
  193. } else {
  194. numbersOfRoom = rand.nextInt((8 - 1) + 1) + 1;
  195. }
  196.  
  197. double fromTheCenter = 15*rand.nextDouble();
  198. DecimalFormat df = new DecimalFormat("#.#");
  199. fromTheCenter = Double.valueOf(df.format(fromTheCenter));
  200. preparedStatement.setInt(1, i);
  201. preparedStatement.setString(2, cities.get(rand.nextInt(cities.size())));
  202. preparedStatement.setString(3, streets.get(rand.nextInt(streets.size())));
  203. preparedStatement.setInt(4, numbersOfRoom);
  204. preparedStatement.setString(5, "22-400");
  205. preparedStatement.setInt(6, surface);
  206. preparedStatement.setDouble(7, fromTheCenter);
  207. preparedStatement.setInt(8,rand.nextInt((2018 - 1980) + 1) + 1980);
  208. preparedStatement.setInt(9,rand.nextInt((11 - 1) + 1) + 1);
  209. preparedStatement.setString(10,rand.nextInt(2) == 1? "t" : "f");
  210. preparedStatement.setString(11, rand.nextInt(2) == 1? "t" : "f");
  211. preparedStatement.setInt(12, rand.nextInt(1200000));
  212. preparedStatement.setInt(13, rand.nextInt(1200000));
  213. preparedStatement.addBatch();
  214. //preparedStatement.execute();
  215. //statement.executeUpdate(insertTableSQL);
  216. //dbConnection.commit();
  217. }
  218.  
  219. preparedStatement.executeBatch();
  220. dbConnection.commit();
  221. System.out.println("Record is inserted into Nieruchomosc table!");
  222.  
  223.  
  224. } catch (SQLException e) {
  225.  
  226. System.out.println(e.getMessage());
  227.  
  228. } finally {
  229.  
  230. if (preparedStatement != null) {
  231. preparedStatement.close();
  232. }
  233.  
  234. if (dbConnection != null) {
  235. dbConnection.close();
  236. }
  237.  
  238. }
  239. System.out.println("STOP Nieruchomosc");
  240. }
  241.  
  242. public static void fillWithDataPracownik() throws SQLException {
  243.  
  244. Connection dbConnection = null;
  245. PreparedStatement preparedStatement = null;
  246. Random rand = new Random();
  247.  
  248.  
  249. try {
  250. dbConnection = getDBConnection();
  251. String insertTableSQL = "INSERT INTO pracownik(idpracownika, imie, nazwisko, stanowisko, pensja, biuro_idbiura) VALUES (?,?,?,?,?,?)";
  252. preparedStatement = dbConnection.prepareStatement(insertTableSQL);
  253.  
  254. for (int i = 0; i < 1500000; i++) {
  255. System.out.println(i);
  256. preparedStatement.setInt(1, i);
  257. preparedStatement.setString(2, "X");
  258. preparedStatement.setString(3, "Y");
  259. preparedStatement.setString(4, position.get(rand.nextInt(position.size())));
  260. preparedStatement.setDouble(5, rand.nextInt((10000 - 2000) + 1) + 2000);
  261. preparedStatement.setInt(6, rand.nextInt(1500000));
  262. preparedStatement.addBatch();
  263. //preparedStatement.execute();
  264. //statement.executeUpdate(insertTableSQL);
  265. //dbConnection.commit();
  266. }
  267.  
  268. preparedStatement.executeBatch();
  269. dbConnection.commit();
  270. System.out.println("Record is inserted into Pracownik table!");
  271.  
  272.  
  273. } catch (SQLException e) {
  274.  
  275. System.out.println(e.getMessage());
  276.  
  277. } finally {
  278.  
  279. if (preparedStatement != null) {
  280. preparedStatement.close();
  281. }
  282.  
  283. if (dbConnection != null) {
  284. dbConnection.close();
  285. }
  286.  
  287. }
  288. System.out.println("STOP Pracownik");
  289.  
  290. }
  291.  
  292. public static void fillWithDataSprzedaz() throws SQLException{
  293. Connection dbConnection = null;
  294. PreparedStatement preparedStatement = null;
  295. Random rand = new Random();
  296.  
  297.  
  298. try {
  299. dbConnection = getDBConnection();
  300. String insertTableSQL = "INSERT INTO sprzedaz(idsprzedazy, datasprzedazy, cena, klient_idklienta, nieruchomosc_idnieruchomosci) VALUES (?,?,?,?,?)";
  301. preparedStatement = dbConnection.prepareStatement(insertTableSQL);
  302.  
  303. for (int i = 0; i < 1500000; i++) {
  304. System.out.println(i);
  305. long offset = Timestamp.valueOf("2009-01-01 00:00:00").getTime();
  306. long end = Timestamp.valueOf("2019-01-01 00:00:00").getTime();
  307. long diff = end - offset + 1;
  308. Timestamp randomDate = new Timestamp(offset + (long)(Math.random() * diff));
  309. preparedStatement.setInt(1, i);
  310. preparedStatement.setTimestamp(2, randomDate);
  311. preparedStatement.setInt(3, rand.nextInt((500000 - 200000) + 1) + 200000);
  312. preparedStatement.setInt(4, rand.nextInt(1200000));
  313. preparedStatement.setInt(5, rand.nextInt(1000000));
  314. preparedStatement.addBatch();
  315. //preparedStatement.execute();
  316. //statement.executeUpdate(insertTableSQL);
  317. //dbConnection.commit();
  318. }
  319.  
  320. preparedStatement.executeBatch();
  321. dbConnection.commit();
  322. System.out.println("Record is inserted into Sprzedaz table!");
  323.  
  324.  
  325. } catch (SQLException e) {
  326.  
  327. System.out.println(e.getMessage());
  328.  
  329. } finally {
  330.  
  331. if (preparedStatement != null) {
  332. preparedStatement.close();
  333. }
  334.  
  335. if (dbConnection != null) {
  336. dbConnection.close();
  337. }
  338.  
  339. }
  340. System.out.println("STOP Sprzedaz");
  341. }
  342.  
  343. public static void fillWithDataWizyta() throws SQLException{
  344. Connection dbConnection = null;
  345. PreparedStatement preparedStatement = null;
  346. Random rand = new Random();
  347.  
  348.  
  349. try {
  350. dbConnection = getDBConnection();
  351. String insertTableSQL = "INSERT INTO wizyta(idwizyty, datawizyty, uwagi, klient_idklienta, nieruchomosc_idnieruchomosci) VALUES (?,?,?,?,?)";
  352. preparedStatement = dbConnection.prepareStatement(insertTableSQL);
  353.  
  354. for (int i = 0; i < 1500000; i++) {
  355. System.out.println(i);
  356. long offset = Timestamp.valueOf("2009-01-01 00:00:00").getTime();
  357. long end = Timestamp.valueOf("2019-01-01 00:00:00").getTime();
  358. long diff = end - offset + 1;
  359. Timestamp randomDate = new Timestamp(offset + (long)(Math.random() * diff));
  360. preparedStatement.setInt(1, i);
  361. preparedStatement.setTimestamp(2, randomDate);
  362. preparedStatement.setString(3, "X");
  363. preparedStatement.setInt(4, rand.nextInt(1200000));
  364. preparedStatement.setInt(5, rand.nextInt(1000000));
  365. preparedStatement.addBatch();
  366. //preparedStatement.execute();
  367. //statement.executeUpdate(insertTableSQL);
  368. //dbConnection.commit();
  369. }
  370.  
  371. preparedStatement.executeBatch();
  372. dbConnection.commit();
  373. System.out.println("Record is inserted into Wizyta table!");
  374.  
  375.  
  376. } catch (SQLException e) {
  377.  
  378. System.out.println(e.getMessage());
  379.  
  380. } finally {
  381.  
  382. if (preparedStatement != null) {
  383. preparedStatement.close();
  384. }
  385.  
  386. if (dbConnection != null) {
  387. dbConnection.close();
  388. }
  389.  
  390. }
  391. System.out.println("STOP Wizyta");
  392. }
  393.  
  394. public static void fillWithDataWlasciciel() throws SQLException {
  395.  
  396. Connection dbConnection = null;
  397. PreparedStatement preparedStatement = null;
  398. Random rand = new Random();
  399.  
  400.  
  401. try {
  402. dbConnection = getDBConnection();
  403. String insertTableSQL = "INSERT INTO wlasciciel(idwlasciciela, imie, nazwisko, miasto, telefon, ulica) VALUES (?,?,?,?,?,?)";
  404. preparedStatement = dbConnection.prepareStatement(insertTableSQL);
  405.  
  406. for (int i = 0; i < 1500000; i++) {
  407. System.out.println(i);
  408. preparedStatement.setInt(1, i);
  409. preparedStatement.setString(2, "X");
  410. preparedStatement.setString(3, "Y");
  411. preparedStatement.setString(4, cities.get(rand.nextInt(cities.size())));
  412. preparedStatement.setString(5, "22-400");
  413. preparedStatement.setString(6, streets.get(rand.nextInt(streets.size())));
  414. preparedStatement.addBatch();
  415. //preparedStatement.execute();
  416. //statement.executeUpdate(insertTableSQL);
  417. //dbConnection.commit();
  418. }
  419.  
  420. preparedStatement.executeBatch();
  421. dbConnection.commit();
  422. System.out.println("Record is inserted into Wlasciciel table!");
  423.  
  424.  
  425. } catch (SQLException e) {
  426.  
  427. System.out.println(e.getMessage());
  428.  
  429. } finally {
  430.  
  431. if (preparedStatement != null) {
  432. preparedStatement.close();
  433. }
  434.  
  435. if (dbConnection != null) {
  436. dbConnection.close();
  437. }
  438.  
  439. }
  440. System.out.println("STOP Wlasciciel");
  441.  
  442.  
  443. }
  444.  
  445. public static void fillWithDataWynajem() throws SQLException{
  446. Connection dbConnection = null;
  447. PreparedStatement preparedStatement = null;
  448. Random rand = new Random();
  449.  
  450.  
  451. try {
  452. dbConnection = getDBConnection();
  453. String insertTableSQL = "INSERT INTO wynajem(idwynajmu, cena, poczatekwynajmu, koniecwynajmu, kaucja, klient_idklienta, nieruchomosc_idnieruchomosci) VALUES (?,?,?,?,?,?,?)";
  454. preparedStatement = dbConnection.prepareStatement(insertTableSQL);
  455.  
  456. for (int i = 0; i < 1500000; i++) {
  457. System.out.println(i);
  458. long offset = Timestamp.valueOf("2009-01-01 00:00:00").getTime();
  459. long end = Timestamp.valueOf("2019-01-01 00:00:00").getTime();
  460. long diff = end - offset + 1;
  461. Timestamp beginningOfRental = new Timestamp(offset + (long)(Math.random() * diff));
  462. ZonedDateTime zonedDateTime = beginningOfRental.toInstant().atZone(ZoneId.of("UTC"));
  463. Timestamp endOfRental = Timestamp.from(zonedDateTime.plus(6, ChronoUnit.MONTHS).toInstant());
  464. preparedStatement.setInt(1, i);
  465. preparedStatement.setInt(2, rand.nextInt((5000 - 500) + 1) + 500);
  466. preparedStatement.setTimestamp(3,beginningOfRental);
  467. preparedStatement.setTimestamp(4,endOfRental);
  468. preparedStatement.setInt(5,rand.nextInt((1500 - 500) + 1) + 500);
  469. preparedStatement.setInt(6, rand.nextInt(1200000));
  470. preparedStatement.setInt(7, rand.nextInt(1000000));
  471. preparedStatement.addBatch();
  472. //preparedStatement.execute();
  473. //statement.executeUpdate(insertTableSQL);
  474. //dbConnection.commit();
  475. }
  476.  
  477. preparedStatement.executeBatch();
  478. dbConnection.commit();
  479. System.out.println("Record is inserted into Wynajem table!");
  480.  
  481.  
  482. } catch (SQLException e) {
  483.  
  484. System.out.println(e.getMessage());
  485.  
  486. } finally {
  487.  
  488. if (preparedStatement != null) {
  489. preparedStatement.close();
  490. }
  491.  
  492. if (dbConnection != null) {
  493. dbConnection.close();
  494. }
  495.  
  496. }
  497. System.out.println("STOP Wynajem");
  498. }
  499. }
Add Comment
Please, Sign In to add comment