-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathPersonDAO.java
More file actions
96 lines (74 loc) · 2.67 KB
/
Copy pathPersonDAO.java
File metadata and controls
96 lines (74 loc) · 2.67 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package ru.alishev.springcourse.dao;
import org.springframework.stereotype.Component;
import ru.alishev.springcourse.models.Person;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
/**
* @author Neil Alishev
*/
@Component
public class PersonDAO {
private static int PEOPLE_COUNT;
private static final String URL = "jdbc:postgresql://localhost:5432/first_db";
private static final String USERNAME = "postgres";
private static final String PASSWORD = "postgres";
private static Connection connection;
static {
try {
Class.forName("org.postgresql.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
public List<Person> index() {
List<Person> people = new ArrayList<>();
try {
Statement statement = connection.createStatement();
String SQL = "SELECT * FROM Person";
ResultSet resultSet = statement.executeQuery(SQL);
while(resultSet.next()) {
Person person = new Person();
person.setId(resultSet.getInt("id"));
person.setName(resultSet.getString("name"));
person.setEmail(resultSet.getString("email"));
person.setAge(resultSet.getInt("age"));
people.add(person);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return people;
}
public Person show(int id) {
// return people.stream().filter(person -> person.getId() == id).findAny().orElse(null);
return null;
}
public void save(Person person) {
// person.setId(++PEOPLE_COUNT);
// people.add(person);
try {
Statement statement = connection.createStatement();
String SQL = "INSERT INTO Person VALUES(" + 1 + ",'" + person.getName() +
"'," + person.getAge() + ",'" + person.getEmail() + "')";
statement.executeUpdate(SQL);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
public void update(int id, Person updatedPerson) {
// Person personToBeUpdated = show(id);
//
// personToBeUpdated.setName(updatedPerson.getName());
// personToBeUpdated.setAge(updatedPerson.getAge());
// personToBeUpdated.setEmail(updatedPerson.getEmail());
}
public void delete(int id) {
// people.removeIf(p -> p.getId() == id);
}
}