-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDepoDAO.java
More file actions
49 lines (45 loc) · 1.76 KB
/
Copy pathDepoDAO.java
File metadata and controls
49 lines (45 loc) · 1.76 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
package Project;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class DepoDAO {
public void addDepo(Depo depo) {
String sql = "INSERT INTO depolar (depo_adi) VALUES (?)";
try (Connection connection = DatabaseConnection.getConnection();
PreparedStatement statement = connection.prepareStatement(sql)) {
statement.setString(1, depo.getDepoAdi());
statement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void deleteDepo(int id) {
String sql = "DELETE FROM depolar WHERE depo_id = ?";
try (Connection connection = DatabaseConnection.getConnection();
PreparedStatement statement = connection.prepareStatement(sql)) {
statement.setInt(1, id);
statement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
public List<Depo> getAllDepolar() {
List<Depo> depolar = new ArrayList<>();
String sql = "SELECT * FROM depolar";
try (Connection connection = DatabaseConnection.getConnection();
PreparedStatement statement = connection.prepareStatement(sql);
ResultSet resultSet = statement.executeQuery()) {
while (resultSet.next()) {
int depoId = resultSet.getInt("depo_id");
String depoAdi = resultSet.getString("depo_adi");
depolar.add(new Depo(depoId, depoAdi));
}
} catch (SQLException e) {
e.printStackTrace();
}
return depolar;
}
}