Advertisement
Guest User

Untitled

a guest
Dec 24th, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.64 KB | None | 0 0
  1. import java.sql.DriverManager;
  2. import java.sql.Connection;
  3. import java.sql.SQLException;
  4. import java.sql.Statement;
  5. import java.text.DateFormat;
  6. import java.text.SimpleDateFormat;
  7. import java.util.concurrent.ThreadFactory;
  8. import java.util.concurrent.ThreadLocalRandom;
  9.  
  10. public class Main {
  11.  
  12.     private static final String DB_DRIVER = "oracle.jdbc.driver.OracleDriver";
  13.     private static final String DB_CONNECTION = "jdbc:oracle:thin:@ora3.elka.pw.edu.pl:1521:ora3inf";
  14.     private static final String DB_USER = "jpawlak";
  15.     private static final String DB_PASSWORD = "jpawlak";
  16.  
  17.  
  18.  
  19.     private static final DateFormat dateFormat = new SimpleDateFormat(
  20.             "yyyy/MM/dd HH:mm:ss");
  21.  
  22.  
  23.     public static String Imie[] = { "OLIVER", "JACK", "HARRY"};
  24.  
  25.  
  26.     public static String Narodowosc[] = {
  27.             "Anglia", "Walia", "Polska", "Szwecja", "Francja", "Hiszpania", "Wlochy", "Azerbejdzan", "Portugalia", "Grecja", "Chorwacja", "Kanada"
  28.     };
  29.  
  30.     public static String Pozycja[] = { "Napastnik", "Bramkarz", "Pomocnik", "Skrzydlowy", "Wahadlowy", "Obronca"};
  31.  
  32.     public static void main(String[] argv) {
  33.  
  34.         Integer id;
  35.         Integer imie;
  36.         Integer nazw;
  37.         Integer nard;
  38.         Integer numKoszulki;
  39.         Integer rok;
  40.         Integer miesiac;
  41.         Integer dzien;
  42.         Integer pozycja;
  43.  
  44.         for(int i = 1; i <= 1000; i++)
  45.         {
  46.             id = i;
  47.             imie = ThreadLocalRandom.current().nextInt(0, 100);
  48.             nazw = ThreadLocalRandom.current().nextInt(0, 100);
  49.             nard = ThreadLocalRandom.current().nextInt(0, 11);
  50.             numKoszulki = ThreadLocalRandom.current().nextInt(1, 30);
  51.             rok =ThreadLocalRandom.current().nextInt(1980, 1997);
  52.             miesiac = ThreadLocalRandom.current().nextInt(1, 12);
  53.             dzien  = ThreadLocalRandom.current().nextInt(1, 27);
  54.             pozycja = ThreadLocalRandom.current().nextInt(0, 5);
  55.  
  56.             try {
  57.  
  58.                 insertRecordIntoDbUserTable(id, Imie[imie], Imie[nazw], rok, miesiac, dzien, numKoszulki, Pozycja[pozycja], Narodowosc[nard]);
  59.  
  60.             } catch (SQLException e) {
  61.  
  62.                 System.out.println(e.getMessage());
  63.  
  64.             }
  65.  
  66.         }
  67.  
  68.  
  69.     }
  70.  
  71.     private static void insertRecordIntoDbUserTable(Integer id, String imie, String nazwisko, Integer rok, Integer miesiac, Integer dzien, Integer numer, String pozycja, String narodowosc)
  72.             throws SQLException {
  73.  
  74.         Connection dbConnection = null;
  75.         Statement statement = null;
  76.  
  77.  
  78.         String insertTableSQL = "INSERT INTO PILKARZ"
  79.                                 + "(PILKARZ_ID, IMIE, NAZWISKO, DATA_URODZENIA, NUMER_KOSZULKI, POZYCJA, NARODOWOSC)"
  80.                                 + "VALUES"
  81.                                 + "("
  82.                                 + id.toString() + ","
  83.                                 + "'" + imie + "'" + ","
  84.                                 + "'" + nazwisko + "'" + ","
  85.                                 + "'" + rok.toString() + "-" + miesiac.toString() + "-" + dzien.toString() + "'" + ","
  86.                                 + numer.toString() + ","
  87.                                 + "'" + pozycja + "'" + ","
  88.                                 + "'" + narodowosc + "'" + ")";
  89.  
  90.  
  91.         try {
  92.             dbConnection = getDBConnection();
  93.             statement = dbConnection.createStatement();
  94.  
  95.             System.out.println(insertTableSQL);
  96.  
  97.             // execute insert SQL statement
  98.             statement.executeUpdate(insertTableSQL);
  99.  
  100.             System.out.println("Record is inserted into your table!");
  101.  
  102.         } catch (SQLException e) {
  103.  
  104.             System.out.println(e.getMessage());
  105.  
  106.         } finally {
  107.  
  108.             if (statement != null) {
  109.                 statement.close();
  110.             }
  111.  
  112.             if (dbConnection != null) {
  113.                 dbConnection.close();
  114.             }
  115.  
  116.         }
  117.  
  118.     }
  119.  
  120.     private static Connection getDBConnection() {
  121.  
  122.         Connection dbConnection = null;
  123.  
  124.         try {
  125.  
  126.             Class.forName(DB_DRIVER);
  127.  
  128.         } catch (ClassNotFoundException e) {
  129.  
  130.             System.out.println(e.getMessage());
  131.  
  132.         }
  133.  
  134.         try {
  135.  
  136.             dbConnection = DriverManager.getConnection(
  137.                     DB_CONNECTION, DB_USER,DB_PASSWORD);
  138.             return dbConnection;
  139.  
  140.         } catch (SQLException e) {
  141.  
  142.             System.out.println(e.getMessage());
  143.  
  144.         }
  145.  
  146.         return dbConnection;
  147.  
  148.     }
  149.  
  150.     private static String getCurrentTimeStamp() {
  151.  
  152.         java.util.Date today = new java.util.Date();
  153.         return dateFormat.format(today.getTime());
  154.  
  155.     }
  156.  
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement