-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.java
81 lines (67 loc) · 2.7 KB
/
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import orm.EntityManager;
import entities.User;
import interfaces.DbContext;
import orm.Connector;
import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.sql.Date;
import java.sql.SQLException;
import java.util.Random;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
public class Main {
private static final String DB_NAME = "soft_uni";
public static void main(String[] args) throws IOException, SQLException, IllegalAccessException, NoSuchMethodException, InvocationTargetException, InstantiationException {
List<String> dbConfig = getDbConfig();
Connector.createConnection(dbConfig.get(0),dbConfig.get(1),DB_NAME);
DbContext<User> entityManager =
new EntityManager<User>(Connector.getConnection());
populateDatabase(entityManager);
List<User> users = entityManager.find(User.class);
List<User> usersWhere = entityManager.find(User.class,"age > 26");
User findFirstUser = entityManager.findFirst(User.class);
User findFirstWhere = entityManager.findFirst(User.class, " age > 26");
System.out.println();
}
public static void populateDatabase(DbContext<User> entityManager) throws SQLException, IllegalAccessException {
Random r = new Random();
int i = -1;
while(i++ < 10){
entityManager.persist(
new User(null,genName(r),r.nextInt(100),
new Date(ThreadLocalRandom.current().nextInt() * 1000L) )
);
}
}
public static String genName(Random r){
String seedName ="";
int k = -1;
while(k++ < 8){
//todo generate random ASCII char ... letters only;
int randomNumber = r.nextInt(122);
while(!((randomNumber >= 65 && randomNumber <= 90) || (randomNumber >= 97 && randomNumber <= 122))){
randomNumber = r.nextInt(122);
}
seedName += Character.toString(randomNumber);
}
return seedName;
}
public static List<String> getDbConfig() throws FileNotFoundException {
List<String> result = new ArrayList<>();
String filePath = "src/main/java/db-conf.env";
File file = new File(filePath);
FileReader fr = new FileReader(file);
try (BufferedReader br = new BufferedReader(fr)) {
var line = br.readLine();
while (line != null) {
result.add(line);
line = br.readLine();
}
} catch (IOException e) {
System.out.println("oopsie something went wrong :: ");
e.printStackTrace();
}
return result;
}
}