Advertisement
Guest User

Untitled

a guest
Jan 24th, 2016
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.77 KB | None | 0 0
  1. /**
  2. * Created by Danutz on 23/01/16.
  3. */
  4.  
  5. import java.sql.*;
  6. import java.text.DateFormat;
  7. import java.text.SimpleDateFormat;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. import java.util.Random;
  11. import java.util.Scanner;
  12. import java.lang.*;
  13. import java.io.*;
  14.  
  15.  
  16. public class JavaInserts {
  17.  
  18. private static Connection con = null;
  19. private static Statement stmt = null;
  20. private static ResultSet res = null;
  21.  
  22. private static String database = "jdbc:oracle:thin:@oracle-lab.cs.univie.ac.at:1521:lab";
  23. private static String user = "a1405569";
  24. private static String pass = "daniel07";
  25.  
  26. // Functions that will generate the specified atribute.
  27.  
  28. private static String generateLocation() {
  29.  
  30. Random rand = new Random();
  31.  
  32. Scanner locationFile = null;
  33. try {
  34. locationFile = new Scanner(new File("Files/Locations")).useDelimiter(",");
  35. } catch (FileNotFoundException e) {
  36. e.printStackTrace();
  37. System.out.println("Because of file reader");
  38. }
  39.  
  40. List<String> locationArray = new ArrayList<String>();
  41.  
  42. while (locationFile.hasNext()) {
  43. // find next line
  44. locationArray.add(locationFile.next());
  45. }
  46. locationFile.close();
  47.  
  48. int randUid = rand.nextInt(locationArray.size());
  49. String location = locationArray.get(randUid);
  50.  
  51. return location;
  52. }
  53.  
  54. private static String plz() {
  55.  
  56. Random rand = new Random();
  57. int randomPlz = (rand.nextInt(10000 - 1000) + 100);
  58. String plz = Integer.toString(randomPlz);
  59.  
  60. return plz;
  61. }
  62.  
  63. private static String homeNumber() {
  64.  
  65. Random rand = new Random();
  66. int randomNr = (rand.nextInt(1000 - 1) + 1);
  67. String homeNumber = Integer.toString(randomNr);
  68.  
  69. return homeNumber;
  70. }
  71.  
  72. private static String firstName() {
  73.  
  74. Random rand = new Random();
  75.  
  76. Scanner firstNameFile = null;
  77. try {
  78. firstNameFile = new Scanner(new File("Files/firstName.txt")).useDelimiter(",");
  79. } catch (FileNotFoundException e) {
  80. e.printStackTrace();
  81. System.out.println("Because of file reader");
  82. }
  83.  
  84. List<String> firstNameArray = new ArrayList<String>();
  85.  
  86. while (firstNameFile.hasNext()) {
  87. // find next line
  88. firstNameArray.add(firstNameFile.next());
  89. }
  90. firstNameFile.close();
  91.  
  92. int randUid = rand.nextInt(firstNameArray.size());
  93. String firstName = firstNameArray.get(randUid);
  94.  
  95. return firstName;
  96. }
  97.  
  98. private static String lastName() {
  99.  
  100. Random rand = new Random();
  101.  
  102. Scanner lastNameFile = null;
  103. try {
  104. lastNameFile = new Scanner(new File("Files/lastName.txt")).useDelimiter(",");
  105. } catch (FileNotFoundException e) {
  106. e.printStackTrace();
  107. System.out.println("Because of file reader");
  108. }
  109.  
  110. List<String> lastNameArray = new ArrayList<String>();
  111.  
  112. while (lastNameFile.hasNext()) {
  113. // find next line
  114. lastNameArray.add(lastNameFile.next());
  115. }
  116. lastNameFile.close();
  117.  
  118. int randUid = rand.nextInt(lastNameArray.size());
  119. String lastName = lastNameArray.get(randUid);
  120.  
  121. return lastName;
  122. }
  123.  
  124. private static String street() {
  125.  
  126. Random rand = new Random();
  127.  
  128. Scanner streetFile = null;
  129. try {
  130. streetFile = new Scanner(new File("Files/streetName.txt")).useDelimiter(",");
  131. } catch (FileNotFoundException e) {
  132. e.printStackTrace();
  133. System.out.println("Because of file reader");
  134. }
  135.  
  136. List<String> streetNameArray = new ArrayList<String>();
  137.  
  138. while (streetFile.hasNext()) {
  139. // find next line
  140. streetNameArray.add(streetFile.next());
  141. }
  142. streetFile.close();
  143.  
  144. int randUid = rand.nextInt(streetNameArray.size());
  145. String street = streetNameArray.get(randUid);
  146.  
  147. return street;
  148. }
  149.  
  150. private static String telefonNr() {
  151.  
  152. Random rand = new Random();
  153.  
  154. Scanner telNrFile = null;
  155. try {
  156. telNrFile = new Scanner(new File("Files/phoneNr.txt")).useDelimiter(",");
  157. } catch (FileNotFoundException e) {
  158. e.printStackTrace();
  159. System.out.println("Because of file reader");
  160. }
  161.  
  162. List<String> teleNrArray = new ArrayList<String>();
  163.  
  164. while (telNrFile.hasNext()) {
  165. // find next line
  166. teleNrArray.add(telNrFile.next());
  167. }
  168. telNrFile.close();
  169.  
  170. int randUid = rand.nextInt(teleNrArray.size());
  171. String telefonNr = teleNrArray.get(randUid);
  172.  
  173. return telefonNr;
  174. }
  175.  
  176. private static String email() {
  177.  
  178. Random rand = new Random();
  179.  
  180. Scanner emailFile = null;
  181. try {
  182. emailFile = new Scanner(new File("Files/emails.txt")).useDelimiter(",");
  183. } catch (FileNotFoundException e) {
  184. e.printStackTrace();
  185. System.out.println("Because of file reader");
  186. }
  187.  
  188. List<String> emailArray = new ArrayList<String>();
  189.  
  190. while (emailFile.hasNext()) {
  191. // find next line
  192. emailArray.add(emailFile.next());
  193. }
  194. emailFile.close();
  195.  
  196. int randUid = rand.nextInt(emailArray.size());
  197. String email = emailArray.get(randUid);
  198.  
  199. return email;
  200. }
  201.  
  202. private static String getColor() {
  203.  
  204.  
  205. Random rand = new Random();
  206.  
  207. Scanner colorFile = null;
  208. try {
  209. colorFile = new Scanner(new File("Files/colors.txt")).useDelimiter(",");
  210. } catch (FileNotFoundException e) {
  211. e.printStackTrace();
  212. System.out.println("Because of file reader");
  213. }
  214.  
  215. List<String> colorArray = new ArrayList<String>();
  216.  
  217. while (colorFile.hasNext()) {
  218. // find next line
  219. colorArray.add(colorFile.next());
  220. }
  221. colorFile.close();
  222.  
  223. int randUid = rand.nextInt(colorArray.size());
  224. String color = colorArray.get(randUid);
  225.  
  226. return color;
  227. }
  228.  
  229. private static String birthDate() {
  230. Random generator = new Random();
  231. int day = generator.nextInt(27) + 1;
  232. int month = generator.nextInt(11) + 1;
  233. int year = 1940 + generator.nextInt(58);
  234. String date = "TO_DATE('" + day + "/" + month + "/" + year + "','DD/MM/YYYY')";
  235.  
  236. return date;
  237.  
  238. }
  239.  
  240. private static String reparation_contract_date() {
  241.  
  242. Random generator = new Random();
  243. int day = generator.nextInt(27) + 1;
  244. int month = generator.nextInt(11) + 1;
  245. int year = 2000 + generator.nextInt(14);
  246. int hour = generator.nextInt(22) + 1;
  247. int min = generator.nextInt(59) + 1;
  248. int s = generator.nextInt(59) + 1;
  249. String date = "TO_DATE('" + day + "/" + month + "/" + year + " " + hour + ":" + min + ":" + s + "', 'DD/MM/YYYY hh24:mi:ss')";
  250.  
  251. return date;
  252. }
  253.  
  254. public static void main(String args[]) throws Exception {
  255.  
  256. int countMitarbeiter = 0;
  257. int countClientNumber = 0;
  258. int countMechanik = 0;
  259. int countKundebetreuer = 0;
  260. int initiation = 10;
  261. int numberOfIns = 2000;
  262.  
  263. //salvate in fisier
  264.  
  265. String car;
  266.  
  267. // Variables with themporary value
  268.  
  269. List<String> temporaryUid = new ArrayList<String>();
  270. List<String> temporaryKfz = new ArrayList<String>();
  271. List<Integer> temporaryMechID = new ArrayList<Integer>();
  272. List<Integer> temporaryBetID = new ArrayList<Integer>();
  273.  
  274.  
  275. try {
  276. Class.forName("oracle.jdbc.driver.OracleDriver");
  277. con = DriverManager.getConnection(database, user, pass);
  278. stmt = con.createStatement();
  279. Random rand = new Random();
  280.  
  281. System.out.println("please enter the table you want to update:");
  282. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  283. System.out.print("Enter String");
  284. String control = br.readLine();
  285.  
  286.  
  287. do {
  288.  
  289. switch ("autovermietungsfirma") {
  290.  
  291. // -------------------- insert Autovermietlungsfirma
  292. case "autovermietungsfirma": {
  293. int randomNumber = (rand.nextInt(1000000000 - 100000000) + 1000000000);
  294. String uid = "\'ATU" + Integer.toString(randomNumber) + "\'";
  295.  
  296. temporaryUid.add(uid);
  297.  
  298.  
  299. Scanner firmaFile = null;
  300. try {
  301. firmaFile = new Scanner(new File("Files/firmaname.txt")).useDelimiter(",");
  302. } catch (FileNotFoundException e) {
  303. e.printStackTrace();
  304. System.out.println("Because of file reader");
  305. }
  306.  
  307. List<String> firmaArrya = new ArrayList<String>();
  308.  
  309. while (firmaFile.hasNext()) {
  310. // find next line
  311. firmaArrya.add(firmaFile.next());
  312. }
  313. firmaFile.close();
  314.  
  315. int randUid = rand.nextInt(firmaArrya.size());
  316. String firmaNamen = firmaArrya.get(randUid);
  317.  
  318. try {
  319.  
  320. String insertIntoAutovermietung = "INSERT INTO Autovermietungsfirma (Umsatzsteuer_id, Name, Land, Ort, PLZ, Strasse, Nummer, Telefon) VALUES (" + uid + ", " + firmaNamen + ", 'Austria' ," + generateLocation() + ", " + plz() + ", " + street() + ", " + homeNumber() + ", " + telefonNr() + ")";
  321.  
  322.  
  323. stmt.executeUpdate(insertIntoAutovermietung);
  324. } catch (Exception e) {
  325. System.err.println("Insert ... + " + e.getMessage());
  326. }
  327.  
  328. }
  329.  
  330.  
  331. // ------------------- insert Into Mitarbeiter
  332. case "mitarbeiter": {
  333.  
  334. do {
  335. countMitarbeiter++;
  336.  
  337. String mitarbeiterType[] = {"'Mechaniker/in'", "'Kundebetreuer/in'"};
  338.  
  339. double salary[] = {1200, 1450, 1700, 2150.99, 2400.84, 3100};
  340. double randSalary = salary[rand.nextInt(6)];
  341.  
  342. int mitarbeiterRandUid = rand.nextInt(temporaryUid.size());
  343. while (mitarbeiterRandUid < 0) {
  344. mitarbeiterRandUid = rand.nextInt(temporaryUid.size());
  345. }
  346. String mitarbeiterUid = temporaryUid.get(mitarbeiterRandUid);
  347.  
  348. int oneORzero;
  349. if (rand.nextBoolean()) {
  350. oneORzero = 1;
  351. } else {
  352. oneORzero = 0;
  353. }
  354.  
  355. String typeOfEmployee = mitarbeiterType[oneORzero];
  356.  
  357. int mitarbeitersChef = 1;
  358.  
  359. if (typeOfEmployee.equals("'Mechaniker/in'")) {
  360. temporaryMechID.add(countMitarbeiter);
  361. if (temporaryMechID.size() > 10) {
  362. mitarbeitersChef = temporaryMechID.get(rand.nextInt(8));
  363. }
  364. countMechanik++;
  365. } else {
  366. temporaryBetID.add(countMitarbeiter);
  367. if (temporaryMechID.size() > 10) {
  368. mitarbeitersChef = temporaryBetID.get(rand.nextInt(8));
  369. }
  370. countKundebetreuer++;
  371. }
  372.  
  373.  
  374. try {
  375.  
  376. String insertIntoMitarbeiter = "INSERT INTO Mitarbeiter (Nachname, Vorname, Geb_datum, Gehalt , Umsatzsteuer_id, Angestelt_als, ChefMNr) VALUES (" + lastName() + ", " + firstName() + ", " + birthDate() + ", " + randSalary + ", " + mitarbeiterUid + ", " + typeOfEmployee + ", " + mitarbeitersChef + ")";
  377.  
  378.  
  379. stmt.executeUpdate(insertIntoMitarbeiter);
  380. } catch (Exception e) {
  381. System.err.println("Insert ... + " + e.getMessage());
  382. }
  383.  
  384.  
  385. // ----------------- Insert into Mechankiker
  386.  
  387. if (typeOfEmployee.equals(mitarbeiterType[0])) {
  388.  
  389. String erfahrung[] = {"'Ja'", "'Nein'", "'Medium'", "'Ich habe fur kurze Zeit in diesem Bereich gearbeitet'", "'Einschulung'"};
  390.  
  391.  
  392. try {
  393.  
  394. String insertIntoMechankiker = "INSERT INTO Mechanik (MID, Erfahrung) VALUES (" + countMitarbeiter + ", " + erfahrung[rand.nextInt(4)] + ")";
  395.  
  396.  
  397. stmt.executeUpdate(insertIntoMechankiker);
  398. } catch (Exception e) {
  399. System.err.println("Insert ... + " + e.getMessage());
  400. }
  401.  
  402.  
  403. } else {
  404.  
  405. // --------------- Insert into Kundebetreuer
  406.  
  407. try {
  408.  
  409. String insertIntoKundebetreuer = "INSERT INTO Kundebetreuer (MID, Telefon, E_Mail) VALUES (" + countMitarbeiter + ", " + telefonNr() + ", " + email() + ")";
  410.  
  411.  
  412. stmt.executeUpdate(insertIntoKundebetreuer);
  413. } catch (Exception e) {
  414. System.err.println("Insert ... + " + e.getMessage());
  415. }
  416.  
  417.  
  418. }
  419. initiation--;
  420. } while (initiation > 0);
  421. }
  422. case "Kunde": {
  423. // -------------- Insert into Kunde
  424.  
  425.  
  426. countClientNumber++;
  427.  
  428. int randBet = rand.nextInt(temporaryBetID.size());
  429. while (randBet < 1) {
  430. randBet = rand.nextInt(temporaryBetID.size());
  431. }
  432.  
  433.  
  434. int bonus = rand.nextInt(100);
  435.  
  436.  
  437. try {
  438. String insertIntoKunde = "INSERT INTO Kunde (Nachname, Vorname, Geb_datum, Telefon, E_Mail, Bonus, MID) VALUES (" + lastName() + ", " + firstName() + ", " + birthDate() + ", " + telefonNr() + ", " + email() + ", " + bonus + ", " + temporaryBetID.get(randBet) + ")";
  439.  
  440.  
  441. stmt.executeUpdate(insertIntoKunde);
  442. } catch (Exception e) {
  443. System.err.println("Insert ... + " + e.getMessage());
  444. }
  445.  
  446. }
  447.  
  448.  
  449. case "auto": {
  450. // ------------ Insert into Auto
  451.  
  452.  
  453. Scanner autoFile = null;
  454. try {
  455. autoFile = new Scanner(new File("Files/Cars/Cars")).useDelimiter(",");
  456. } catch (FileNotFoundException e) {
  457. e.printStackTrace();
  458. System.out.println("Because of file reader");
  459. }
  460.  
  461. List<String> autoArray = new ArrayList<String>();
  462.  
  463. while (autoFile.hasNext()) {
  464. // find next line
  465. autoArray.add(autoFile.next());
  466. }
  467. autoFile.close();
  468.  
  469. int randUid = rand.nextInt(autoArray.size());
  470. while (randUid < 1) {
  471. randUid = rand.nextInt(autoArray.size());
  472. }
  473. car = autoArray.get(randUid);
  474.  
  475.  
  476. Scanner inSecondFile = null;
  477. try {
  478. inSecondFile = new Scanner(new File("Files/Cars/" + car)).useDelimiter(",");
  479. } catch (FileNotFoundException e) {
  480. e.printStackTrace();
  481. System.out.println("Because of file reader");
  482. }
  483.  
  484. List<String> temps2 = new ArrayList<String>();
  485.  
  486. while (inSecondFile.hasNext()) {
  487. // find next line
  488. temps2.add(inSecondFile.next());
  489. }
  490. inSecondFile.close();
  491.  
  492. int randModel = rand.nextInt(temps2.size());
  493. while (randModel < 1) {
  494. randModel = rand.nextInt(temps2.size());
  495. }
  496. String model = temps2.get(randModel);
  497.  
  498.  
  499. int randNumber = (rand.nextInt(1000000 - 100000) + 100000);
  500. String kfz = "\'W" + Integer.toString(randNumber) + "\'";
  501. temporaryKfz.add(kfz);
  502.  
  503.  
  504. int autoRandUid = rand.nextInt((temporaryUid.size() - 1) + 1);
  505. while (temporaryUid.size() < 1) {
  506. autoRandUid = 1;
  507. }
  508. String autoUid = temporaryUid.get(autoRandUid);
  509.  
  510. int kundeRandomID = rand.nextInt(countClientNumber);
  511. while (kundeRandomID < 1) {
  512. kundeRandomID = 1;
  513. }
  514. double randomSize = rand.nextInt(4 - 1) + 2;
  515. int randomPlaces = rand.nextInt(7 - 2) + 2;
  516.  
  517. try {
  518. String insertIntoAuto = "INSERT INTO Auto (KfzKennzeichen, Farbe, Große, Platze, Brand, Model, Umsatzsteuer_id, KundeID) VALUES (" + kfz + ", " + getColor() + ", " + randomSize + ", " + randomPlaces + ", " + "'" + car + "'" + ", " + model + ", " + autoUid + ", " + kundeRandomID + ")";
  519.  
  520. stmt.executeUpdate(insertIntoAuto);
  521. } catch (Exception e) {
  522. System.err.println("Insert ... + " + e.getMessage());
  523. }
  524. }
  525.  
  526. // ----------- Insert Into reparatur
  527. case "reparatur": {
  528.  
  529. int reparaturRandKfz = rand.nextInt(temporaryKfz.size());
  530. while (temporaryKfz.size() < 1) {
  531. reparaturRandKfz = 1;
  532. }
  533. String reparaturKfz = temporaryKfz.get(reparaturRandKfz);
  534.  
  535.  
  536. int randMech = temporaryMechID.get(rand.nextInt(countMechanik));
  537.  
  538. int dauern = rand.nextInt(200);
  539.  
  540.  
  541. String reparationType[] = {"'Abgasuntersuchung'", "'Achsvermessung'", "'Auswuchten'", "'Anhängerkupplung'", "'Autobatterie'", "'Autoreifen'", "'Bremsen'", "'Hauptuntersuchung'", "'Hebebühne'", "'Lackdoktor'", "'Klimaservice\'", "\'Keilriemen\'"};
  542.  
  543. try {
  544.  
  545. String insertIntoKReparatur = "INSERT INTO Reparatur (KfzKennzeichen, MID, Datum, Dauern, Reparaturtype) VALUES (" + reparaturKfz + ", " + randMech + ", " + reparation_contract_date() + ", " + dauern + ", " + reparationType[rand.nextInt(11)] + ")";
  546.  
  547. stmt.executeUpdate(insertIntoKReparatur);
  548. } catch (Exception e) {
  549. System.err.println("Insert ... + " + e.getMessage());
  550. }
  551.  
  552. }
  553. // ---------- Insert Into Vertrag
  554. case "vertrag": {
  555.  
  556. int randBet = temporaryBetID.get(rand.nextInt(countKundebetreuer));
  557.  
  558. String zahlungsart[] = {"'Bar'", "'Kreditkarte'", "'Paypal'"};
  559.  
  560. double randomPreis = (rand.nextInt(4000 - 30) + 30);
  561.  
  562. try {
  563.  
  564. String insertIntoVertrag = "INSERT INTO Vertrag (MID, KundeID, Datum, Preis, Zahlungsart) VALUES (" + randBet + ", " + countClientNumber + ", " + reparation_contract_date() + ", " + randomPreis + ", " + zahlungsart[2] + ")";
  565. System.out.println(insertIntoVertrag);
  566.  
  567. stmt.executeUpdate(insertIntoVertrag);
  568. } catch (Exception e) {
  569. System.err.println("Insert ... + " + e.getMessage());
  570. }
  571.  
  572. }
  573. break;
  574.  
  575. numberOfIns--;
  576. }
  577. while (numberOfIns > 0) ;
  578.  
  579. /* try {
  580. Class.forName("oracle.jdbc.driver.OracleDriver");
  581. con = DriverManager.getConnection(database, user, pass);
  582. stmt = con.createStatement();
  583.  
  584. try {
  585. String innsertInto = "INSERT INTO Autovermietungsfirma VALUES ('ATU144171777',' Rent a car','Austria','Wien','1020','Obere Augartenstrße','12A','+43 673351112')";
  586.  
  587. stmt.executeUpdate(innsertInto);
  588. } catch (Exception e) {
  589. System.err.println("Insert ... + " + e.getMessage());
  590. }
  591.  
  592.  
  593. res = stmt.executeQuery("SELECT Name FROM Autovermietungsfirma WHERE (Umsatzsteuer_id = 'ATU214356546')");
  594. if (res.next()) {
  595. String nameOf = res.getString(1);
  596. System.out.println("Name = " + nameOf);
  597. }
  598.  
  599. try {
  600. con.close();
  601. stmt.close();
  602. res.close();
  603.  
  604.  
  605. } catch (Exception e) {
  606. System.out.println("End ... + " + e.getMessage());
  607. }
  608. }
  609. catch (Exception e) {
  610. System.err.println("Connection ... + " + e.getMessage());
  611. }*/
  612.  
  613.  
  614. try {
  615. con.close();
  616. stmt.close();
  617. res.close();
  618.  
  619. } catch (Exception e) {
  620. System.out.println("End ... + " + e.getMessage());
  621. }
  622.  
  623. }
  624.  
  625. } catch (Exception e) {
  626. System.err.println("Connection ... + " + e.getMessage());
  627. }
  628.  
  629. }
  630. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement