Repository navigation
Expand file tree
/
Copy path28-StudentDaoPattern.java
More file actions
46 lines (41 loc) · 1.58 KB
/
Copy path28-StudentDaoPattern.java
File metadata and controls
46 lines (41 loc) · 1.58 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
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
class StudentDao {
private final Connection connection;
StudentDao(Connection connection) {
this.connection = connection;
}
void add(String name, int marks) throws Exception {
try (PreparedStatement statement = connection.prepareStatement(
"INSERT INTO students(name, marks) VALUES (?, ?)")) {
statement.setString(1, name);
statement.setInt(2, marks);
statement.executeUpdate();
}
}
void findAll() throws Exception {
try (PreparedStatement statement =
connection.prepareStatement("SELECT id, name, marks FROM students");
ResultSet result = statement.executeQuery()) {
while (result.next()) {
System.out.println(result.getInt("id") + " "
+ result.getString("name") + " "
+ result.getInt("marks"));
}
}
}
}
class StudentDaoPattern {
public static void main(String[] args) throws Exception {
String url = System.getenv().getOrDefault("MYSQL_URL", "jdbc:mysql://localhost:3306/studentdb");
try (Connection connection = DriverManager.getConnection(url,
System.getenv().getOrDefault("MYSQL_USER", "root"),
System.getenv().getOrDefault("MYSQL_PASSWORD", ""))) {
StudentDao dao = new StudentDao(connection);
dao.add("DAO Student", 88);
dao.findAll();
}
}
}