feat: decoupled donors generation from main
This commit is contained in:
parent
fdaf9c3bdf
commit
15c1cc4f18
Binary file not shown.
Before Width: | Height: | Size: 248 KiB After Width: | Height: | Size: 229 KiB |
122
src/main/java/org/example/DonorsGenerator.java
Normal file
122
src/main/java/org/example/DonorsGenerator.java
Normal file
@ -0,0 +1,122 @@
|
|||||||
|
package org.example;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.Calendar;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
public class DonorsGenerator {
|
||||||
|
static final String[] CITIES = {"Montreal", "Laval", "Brossard"};
|
||||||
|
static final String[] GENDERS = {"M", "F", "U"};
|
||||||
|
|
||||||
|
private static final String[] STREET_NAMES = {
|
||||||
|
"Pine", "Oak", "Lakeview", "Sunset", "Forest", "River", "Spring",
|
||||||
|
"Main", "First", "Wind", "Park", "Maple", "Elm", "Cerulean"
|
||||||
|
};
|
||||||
|
protected static final String[] HOBBIES = {
|
||||||
|
"Hiking", "Skiing", "Swimming", "Bird watching", "Racing", "Walking", "Gaming",
|
||||||
|
};
|
||||||
|
|
||||||
|
private final Random RANDOM;
|
||||||
|
|
||||||
|
private static Connection conn;
|
||||||
|
|
||||||
|
private final int count;
|
||||||
|
|
||||||
|
public DonorsGenerator(int recordCount, Connection conn, Random random) {
|
||||||
|
DonorsGenerator.conn = conn;
|
||||||
|
count = recordCount;
|
||||||
|
RANDOM = random;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void insertDonors() throws SQLException {
|
||||||
|
String sql = "INSERT INTO Donors (donorID, firstName, lastName, middleInitial, dateOfBirth, address, city, postalCode, province, gender, SSN, hobby, phone, email) " +
|
||||||
|
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
|
||||||
|
|
||||||
|
PreparedStatement pstmt = conn.prepareStatement(sql);
|
||||||
|
|
||||||
|
for (int i = 1; i <= count; i++) {
|
||||||
|
pstmt.setInt(1, i); // donorID
|
||||||
|
pstmt.setString(2, randomString(5));
|
||||||
|
pstmt.setString(3, randomString(7));
|
||||||
|
pstmt.setString(4, randomInitial());
|
||||||
|
pstmt.setDate(5, randomDateOfBirth());
|
||||||
|
pstmt.setString(6, randomAddress());
|
||||||
|
pstmt.setString(7, randomCities()); // city (static data for simplicity)
|
||||||
|
pstmt.setString(8, randomPostalCode()); // postalCode (static data for simplicity)
|
||||||
|
pstmt.setString(9, "QC"); // province
|
||||||
|
pstmt.setString(10, randomGender()); // gender
|
||||||
|
pstmt.setString(11, randomSSN()); // SSN
|
||||||
|
pstmt.setString(12, randomHobbies()); // hobby (static data for simplicity)
|
||||||
|
pstmt.setString(13, "1234567890"); // phone (static data for simplicity)
|
||||||
|
pstmt.setString(14, "email" + i + "@example.com"); // email (static data for simplicity)
|
||||||
|
|
||||||
|
pstmt.addBatch();
|
||||||
|
}
|
||||||
|
|
||||||
|
pstmt.executeBatch(); // Execute all inserts as a batch
|
||||||
|
pstmt.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String randomString(int length) {
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
for (int i = 0; i < length; i++) {
|
||||||
|
char c = (char) (RANDOM.nextInt(26) + 'a');
|
||||||
|
sb.append(c);
|
||||||
|
}
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String randomInitial() {
|
||||||
|
return RANDOM.nextBoolean() ? String.valueOf((char) (RANDOM.nextInt(26) + 'a')) : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static java.sql.Date randomDateOfBirth() {
|
||||||
|
Calendar calendar = Calendar.getInstance();
|
||||||
|
calendar.set(Calendar.YEAR, 1975 + RANDOM.nextInt(50));
|
||||||
|
calendar.set(Calendar.MONTH, RANDOM.nextInt(12));
|
||||||
|
calendar.set(Calendar.DAY_OF_MONTH, RANDOM.nextInt(28) + 1);
|
||||||
|
return new java.sql.Date(calendar.getTimeInMillis());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String randomAddress() {
|
||||||
|
int civicNumber = RANDOM.nextInt(1000) + 1; // Random number between 1 and 1000
|
||||||
|
String streetName = STREET_NAMES[RANDOM.nextInt(STREET_NAMES.length)]; // Random street name
|
||||||
|
|
||||||
|
return civicNumber + " " + streetName + " Street";
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String randomGender() {
|
||||||
|
return GENDERS[RANDOM.nextInt(GENDERS.length)];
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String randomCities() {
|
||||||
|
return CITIES[RANDOM.nextInt(CITIES.length)];
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String randomHobbies() {
|
||||||
|
return HOBBIES[RANDOM.nextInt(HOBBIES.length)];
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String randomSSN() {
|
||||||
|
return (RANDOM.nextInt(999) + 1) + "-" +
|
||||||
|
(RANDOM.nextInt(999) + 1) + "-" +
|
||||||
|
(RANDOM.nextInt(999) + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String randomPostalCode() {
|
||||||
|
|
||||||
|
// Generate first segment (letter digit letter)
|
||||||
|
|
||||||
|
return String.valueOf((char) (RANDOM.nextInt(26) + 'A')) +
|
||||||
|
RANDOM.nextInt(10) +
|
||||||
|
(char) (RANDOM.nextInt(26) + 'A') +
|
||||||
|
" " +
|
||||||
|
|
||||||
|
// Generate second segment (digit letter digit)
|
||||||
|
RANDOM.nextInt(10) +
|
||||||
|
(char) (RANDOM.nextInt(26) + 'A') +
|
||||||
|
RANDOM.nextInt(10);
|
||||||
|
}
|
||||||
|
}
|
@ -1,39 +1,31 @@
|
|||||||
package org.example;
|
package org.example;
|
||||||
|
|
||||||
import java.sql.*;
|
import java.sql.Connection;
|
||||||
|
import java.sql.DriverManager;
|
||||||
|
import java.sql.SQLException;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
import java.util.Calendar;
|
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
// change the DB_URL accordingly
|
// change the DB_URL accordingly
|
||||||
static final String HOST = "localhost";
|
static final String HOST = "localhost";
|
||||||
static final String PORT = "32776";
|
static final String PORT = "32772";
|
||||||
static final String DATABASE_NAME = "assignment1";
|
static final String DATABASE_NAME = "assignment1";
|
||||||
|
|
||||||
static final String DB_URL = "jdbc:mysql://" + HOST + ":" + PORT + "/" + DATABASE_NAME;
|
static final String DB_URL = "jdbc:mysql://" + HOST + ":" + PORT + "/" + DATABASE_NAME;
|
||||||
static final String USER = "root";
|
static final String USER = "root";
|
||||||
static final String PASSWORD = "secret";
|
static final String PASSWORD = "secret";
|
||||||
|
|
||||||
static final String[] PROVINCES = {"AB", "BC", "MB", "NB", "NL", "NS", "NT", "NU", "ON", "PE", "QC", "SK", "YT"};
|
|
||||||
static final String[] CITIES = {"Montreal", "Laval", "Brossard"};
|
|
||||||
static final String[] GENDERS = {"M", "F", "U"};
|
|
||||||
|
|
||||||
private static final String[] STREET_NAMES = {
|
|
||||||
"Pine", "Oak", "Lakeview", "Sunset", "Forest", "River", "Spring",
|
|
||||||
"Main", "First", "Wind", "Park", "Maple", "Elm", "Cerulean"
|
|
||||||
};
|
|
||||||
private static final String[] HOBBIES = {
|
|
||||||
"Hiking", "Skiing", "Swiming", "Bird watching", "Racing", "Walking", "Gaming",
|
|
||||||
};
|
|
||||||
|
|
||||||
private static final Random RANDOM = new Random();
|
private static final Random RANDOM = new Random();
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
try {
|
try {
|
||||||
int numOfDonors = 5000;
|
|
||||||
|
|
||||||
Connection conn = DriverManager.getConnection(DB_URL, USER, PASSWORD);
|
Connection conn = DriverManager.getConnection(DB_URL, USER, PASSWORD);
|
||||||
insertDonors(conn, numOfDonors);
|
|
||||||
|
// generate 500 Donors
|
||||||
|
DonorsGenerator donorsGenerator = new DonorsGenerator(500, conn, RANDOM);
|
||||||
|
|
||||||
|
donorsGenerator.insertDonors();
|
||||||
|
|
||||||
conn.close();
|
conn.close();
|
||||||
|
|
||||||
System.out.println("Insertions to Donors were successful");
|
System.out.println("Insertions to Donors were successful");
|
||||||
@ -41,104 +33,4 @@ public class Main {
|
|||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void insertDonors(Connection conn, int count) throws SQLException {
|
|
||||||
String sql = "INSERT INTO Donors (donorID, firstName, lastName, middleInitial, dateOfBirth, address, city, postalCode, province, gender, SSN, hobby, phone, email) " +
|
|
||||||
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
|
|
||||||
|
|
||||||
PreparedStatement pstmt = conn.prepareStatement(sql);
|
|
||||||
|
|
||||||
for (int i = 1; i <= count; i++) {
|
|
||||||
pstmt.setInt(1, i); // donorID
|
|
||||||
pstmt.setString(2, randomString(5));
|
|
||||||
pstmt.setString(3, randomString(7));
|
|
||||||
pstmt.setString(4, randomInitial());
|
|
||||||
pstmt.setDate(5, randomDateOfBirth());
|
|
||||||
pstmt.setString(6, randomAddress());
|
|
||||||
pstmt.setString(7, randomCities()); // city (static data for simplicity)
|
|
||||||
pstmt.setString(8, randomPostalCode()); // postalCode (static data for simplicity)
|
|
||||||
pstmt.setString(9, randomProvince()); // province
|
|
||||||
pstmt.setString(10, randomGender()); // gender
|
|
||||||
pstmt.setString(11, randomSSN()); // SSN
|
|
||||||
pstmt.setString(12, randomHobbies()); // hobby (static data for simplicity)
|
|
||||||
pstmt.setString(13, "1234567890"); // phone (static data for simplicity)
|
|
||||||
pstmt.setString(14, "email" + i + "@example.com"); // email (static data for simplicity)
|
|
||||||
|
|
||||||
pstmt.addBatch();
|
|
||||||
}
|
|
||||||
|
|
||||||
pstmt.executeBatch(); // Execute all inserts as a batch
|
|
||||||
pstmt.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
private static String randomString(int length) {
|
|
||||||
StringBuilder sb = new StringBuilder();
|
|
||||||
for (int i = 0; i < length; i++) {
|
|
||||||
char c = (char) (RANDOM.nextInt(26) + 'a');
|
|
||||||
sb.append(c);
|
|
||||||
}
|
|
||||||
return sb.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
private static String randomInitial() {
|
|
||||||
return RANDOM.nextBoolean() ? String.valueOf((char) (RANDOM.nextInt(26) + 'a')) : null;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static java.sql.Date randomDateOfBirth() {
|
|
||||||
Calendar calendar = Calendar.getInstance();
|
|
||||||
calendar.set(Calendar.YEAR, 1950 + RANDOM.nextInt(50)); // Random year between 1950 and 1999
|
|
||||||
calendar.set(Calendar.MONTH, RANDOM.nextInt(12)); // Random month
|
|
||||||
calendar.set(Calendar.DAY_OF_MONTH, RANDOM.nextInt(28) + 1); // Random day (considering 28 days in a month)
|
|
||||||
return new java.sql.Date(calendar.getTimeInMillis());
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String randomAddress() {
|
|
||||||
int civicNumber = RANDOM.nextInt(1000) + 1; // Random number between 1 and 1000
|
|
||||||
String streetName = STREET_NAMES[RANDOM.nextInt(STREET_NAMES.length)]; // Random street name
|
|
||||||
|
|
||||||
return civicNumber + " " + streetName + " Street";
|
|
||||||
}
|
|
||||||
|
|
||||||
private static String randomProvince() {
|
|
||||||
return PROVINCES[RANDOM.nextInt(PROVINCES.length)];
|
|
||||||
}
|
|
||||||
|
|
||||||
private static String randomGender() {
|
|
||||||
return GENDERS[RANDOM.nextInt(GENDERS.length)];
|
|
||||||
}
|
|
||||||
|
|
||||||
private static String randomCities() {
|
|
||||||
return CITIES[RANDOM.nextInt(CITIES.length)];
|
|
||||||
}
|
|
||||||
|
|
||||||
private static String randomHobbies() {
|
|
||||||
return HOBBIES[RANDOM.nextInt(HOBBIES.length)];
|
|
||||||
}
|
|
||||||
|
|
||||||
private static String randomSSN() {
|
|
||||||
StringBuilder sb = new StringBuilder();
|
|
||||||
sb.append(RANDOM.nextInt(999) + 1).append("-");
|
|
||||||
sb.append(RANDOM.nextInt(99) + 1).append("-");
|
|
||||||
sb.append(RANDOM.nextInt(9999) + 1);
|
|
||||||
return sb.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String randomPostalCode() {
|
|
||||||
StringBuilder postalCode = new StringBuilder();
|
|
||||||
|
|
||||||
// Generate first segment (letter digit letter)
|
|
||||||
postalCode.append((char) (RANDOM.nextInt(26) + 'A')); // First letter (A-Z)
|
|
||||||
postalCode.append(RANDOM.nextInt(10)); // First digit (0-9)
|
|
||||||
postalCode.append((char) (RANDOM.nextInt(26) + 'A')); // Second letter (A-Z)
|
|
||||||
|
|
||||||
// Space separator (optional, in the Canadian postal code format)
|
|
||||||
postalCode.append(" ");
|
|
||||||
|
|
||||||
// Generate second segment (digit letter digit)
|
|
||||||
postalCode.append(RANDOM.nextInt(10)); // First digit (0-9)
|
|
||||||
postalCode.append((char) (RANDOM.nextInt(26) + 'A')); // First letter (A-Z)
|
|
||||||
postalCode.append(RANDOM.nextInt(10)); // Second digit (0-9)
|
|
||||||
|
|
||||||
return postalCode.toString();
|
|
||||||
}
|
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user