Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.DriverManager;
- import java.sql.Connection;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.text.DateFormat;
- import java.text.SimpleDateFormat;
- import java.util.concurrent.ThreadFactory;
- import java.util.concurrent.ThreadLocalRandom;
- public class Main {
- private static final String DB_DRIVER = "oracle.jdbc.driver.OracleDriver";
- private static final String DB_CONNECTION = "jdbc:oracle:thin:@ora3.elka.pw.edu.pl:1521:ora3inf";
- private static final String DB_USER = "jpawlak";
- private static final String DB_PASSWORD = "jpawlak";
- private static final DateFormat dateFormat = new SimpleDateFormat(
- "yyyy/MM/dd HH:mm:ss");
- public static String Imie[] = { "OLIVER", "JACK", "HARRY"};
- public static String Narodowosc[] = {
- "Anglia", "Walia", "Polska", "Szwecja", "Francja", "Hiszpania", "Wlochy", "Azerbejdzan", "Portugalia", "Grecja", "Chorwacja", "Kanada"
- };
- public static String Pozycja[] = { "Napastnik", "Bramkarz", "Pomocnik", "Skrzydlowy", "Wahadlowy", "Obronca"};
- public static void main(String[] argv) {
- Integer id;
- Integer imie;
- Integer nazw;
- Integer nard;
- Integer numKoszulki;
- Integer rok;
- Integer miesiac;
- Integer dzien;
- Integer pozycja;
- for(int i = 1; i <= 1000; i++)
- {
- id = i;
- imie = ThreadLocalRandom.current().nextInt(0, 100);
- nazw = ThreadLocalRandom.current().nextInt(0, 100);
- nard = ThreadLocalRandom.current().nextInt(0, 11);
- numKoszulki = ThreadLocalRandom.current().nextInt(1, 30);
- rok =ThreadLocalRandom.current().nextInt(1980, 1997);
- miesiac = ThreadLocalRandom.current().nextInt(1, 12);
- dzien = ThreadLocalRandom.current().nextInt(1, 27);
- pozycja = ThreadLocalRandom.current().nextInt(0, 5);
- try {
- insertRecordIntoDbUserTable(id, Imie[imie], Imie[nazw], rok, miesiac, dzien, numKoszulki, Pozycja[pozycja], Narodowosc[nard]);
- } catch (SQLException e) {
- System.out.println(e.getMessage());
- }
- }
- }
- private static void insertRecordIntoDbUserTable(Integer id, String imie, String nazwisko, Integer rok, Integer miesiac, Integer dzien, Integer numer, String pozycja, String narodowosc)
- throws SQLException {
- Connection dbConnection = null;
- Statement statement = null;
- String insertTableSQL = "INSERT INTO PILKARZ"
- + "(PILKARZ_ID, IMIE, NAZWISKO, DATA_URODZENIA, NUMER_KOSZULKI, POZYCJA, NARODOWOSC)"
- + "VALUES"
- + "("
- + id.toString() + ","
- + "'" + imie + "'" + ","
- + "'" + nazwisko + "'" + ","
- + "'" + rok.toString() + "-" + miesiac.toString() + "-" + dzien.toString() + "'" + ","
- + numer.toString() + ","
- + "'" + pozycja + "'" + ","
- + "'" + narodowosc + "'" + ")";
- try {
- dbConnection = getDBConnection();
- statement = dbConnection.createStatement();
- System.out.println(insertTableSQL);
- // execute insert SQL statement
- statement.executeUpdate(insertTableSQL);
- System.out.println("Record is inserted into your table!");
- } catch (SQLException e) {
- System.out.println(e.getMessage());
- } finally {
- if (statement != null) {
- statement.close();
- }
- if (dbConnection != null) {
- dbConnection.close();
- }
- }
- }
- private static Connection getDBConnection() {
- Connection dbConnection = null;
- try {
- Class.forName(DB_DRIVER);
- } catch (ClassNotFoundException e) {
- System.out.println(e.getMessage());
- }
- try {
- dbConnection = DriverManager.getConnection(
- DB_CONNECTION, DB_USER,DB_PASSWORD);
- return dbConnection;
- } catch (SQLException e) {
- System.out.println(e.getMessage());
- }
- return dbConnection;
- }
- private static String getCurrentTimeStamp() {
- java.util.Date today = new java.util.Date();
- return dateFormat.format(today.getTime());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement