|
1 |
| -package org.example.DAO; |
| 1 | +package org.example.dao; |
2 | 2 |
|
3 | 3 | import org.example.models.Envoyer;
|
| 4 | +import org.example.util.Config; |
| 5 | + |
4 | 6 | import java.sql.*;
|
5 | 7 | import java.util.ArrayList;
|
6 | 8 | import java.util.List;
|
7 | 9 |
|
8 | 10 | public class EnvoyerDao {
|
9 | 11 |
|
10 |
| - private Connection connect() throws SQLException { |
11 |
| - String url = "jdbc:postgresql://localhost:5432/cash"; |
12 |
| - String user = "postgres"; |
13 |
| - String password = "mario123"; |
14 |
| - return DriverManager.getConnection(url, user, password); |
15 |
| - } |
| 12 | + private Connection connect() throws SQLException { |
| 13 | + String url = Config.get("db.url"); |
| 14 | + String user = Config.get("db.user"); |
| 15 | + String password = Config.get("db.password"); |
| 16 | + return DriverManager.getConnection(url, user, password); |
| 17 | + } |
16 | 18 |
|
17 |
| - public List<Envoyer> listAllEnvois() throws SQLException { |
18 |
| - List<Envoyer> listEnvoyer = new ArrayList<>(); |
19 |
| - try (Connection conn = connect(); |
20 |
| - Statement stmt = conn.createStatement(); |
21 |
| - ResultSet rs = stmt.executeQuery("SELECT * FROM ENVOYER")) { |
22 |
| - while (rs.next()) { |
23 |
| - int idEnv = rs.getInt("idEnv"); |
24 |
| - String numEnvoyeur = rs.getString("numEnvoyeur"); |
25 |
| - String numRecepteur = rs.getString("numRecepteur"); |
26 |
| - int montant = rs.getInt("montant"); |
27 |
| - Date date = rs.getDate("date"); |
28 |
| - String raison = rs.getString("raison"); |
29 |
| - listEnvoyer.add(new Envoyer(idEnv, numEnvoyeur, numRecepteur, montant, date, raison)); |
30 |
| - } |
| 19 | + public List<Envoyer> listAllEnvois() throws SQLException { |
| 20 | + List<Envoyer> listEnvoyer = new ArrayList<>(); |
| 21 | + try (Connection conn = connect(); |
| 22 | + Statement stmt = conn.createStatement(); |
| 23 | + ResultSet rs = stmt.executeQuery("SELECT * FROM ENVOYER")) { |
| 24 | + |
| 25 | + while (rs.next()) { |
| 26 | + int idEnv = rs.getInt("idEnv"); |
| 27 | + String numEnvoyeur = rs.getString("numEnvoyeur"); |
| 28 | + String numRecepteur = rs.getString("numRecepteur"); |
| 29 | + int montant = rs.getInt("montant"); |
| 30 | + Date date = rs.getDate("date"); |
| 31 | + String raison = rs.getString("raison"); |
| 32 | + listEnvoyer.add(new Envoyer(idEnv, numEnvoyeur, numRecepteur, montant, date, raison)); |
31 | 33 | }
|
32 |
| - return listEnvoyer; |
33 | 34 | }
|
| 35 | + return listEnvoyer; |
| 36 | + } |
34 | 37 |
|
35 |
| - public void insertEnvoyer(Envoyer envoyer) throws SQLException { |
36 |
| - try (Connection conn = connect(); |
37 |
| - PreparedStatement pstmt = conn.prepareStatement("INSERT INTO ENVOYER (numEnvoyeur, numRecepteur, montant, date, raison) VALUES (?, ?, ?, ?, ?)")) { |
38 |
| - pstmt.setString(1, envoyer.getNumEnvoyeur()); |
39 |
| - pstmt.setString(2, envoyer.getNumRecepteur()); |
40 |
| - pstmt.setInt(3, envoyer.getMontant()); |
41 |
| - pstmt.setTimestamp(4, new Timestamp(envoyer.getDate().getTime())); |
42 |
| - pstmt.setString(5, envoyer.getRaison()); |
43 |
| - pstmt.executeUpdate(); |
44 |
| - } |
| 38 | + public void insertEnvoyer(Envoyer envoyer) throws SQLException { |
| 39 | + try (Connection conn = connect(); |
| 40 | + PreparedStatement pstmt = conn.prepareStatement("INSERT INTO ENVOYER (numEnvoyeur, numRecepteur, montant, date, raison) VALUES (?, ?, ?, ?, ?)")) { |
| 41 | + pstmt.setString(1, envoyer.numEnvoyeur()); |
| 42 | + pstmt.setString(2, envoyer.numRecepteur()); |
| 43 | + pstmt.setInt(3, envoyer.montant()); |
| 44 | + pstmt.setTimestamp(4, new Timestamp(envoyer.date().getTime())); |
| 45 | + pstmt.setString(5, envoyer.raison()); |
| 46 | + pstmt.executeUpdate(); |
45 | 47 | }
|
| 48 | + } |
46 | 49 |
|
47 |
| - public Envoyer getEnvoyer(int idEnv) throws SQLException { |
48 |
| - Envoyer envoyer = null; |
49 |
| - try (Connection conn = connect(); |
50 |
| - PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM ENVOYER WHERE idEnv = ?")) { |
51 |
| - pstmt.setInt(1, idEnv); |
52 |
| - ResultSet rs = pstmt.executeQuery(); |
53 |
| - if (rs.next()) { |
54 |
| - String numEnvoyeur = rs.getString("numEnvoyeur"); |
55 |
| - String numRecepteur = rs.getString("numRecepteur"); |
56 |
| - int montant = rs.getInt("montant"); |
57 |
| - Date date = rs.getDate("date"); |
58 |
| - String raison = rs.getString("raison"); |
59 |
| - envoyer = new Envoyer(idEnv, numEnvoyeur, numRecepteur, montant, date, raison); |
60 |
| - } |
| 50 | + public Envoyer getEnvoyer(int idEnv) throws SQLException { |
| 51 | + Envoyer envoyer = null; |
| 52 | + try (Connection conn = connect(); |
| 53 | + PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM ENVOYER WHERE idEnv = ?")) { |
| 54 | + pstmt.setInt(1, idEnv); |
| 55 | + ResultSet rs = pstmt.executeQuery(); |
| 56 | + if (rs.next()) { |
| 57 | + String numEnvoyeur = rs.getString("numEnvoyeur"); |
| 58 | + String numRecepteur = rs.getString("numRecepteur"); |
| 59 | + int montant = rs.getInt("montant"); |
| 60 | + Date date = rs.getDate("date"); |
| 61 | + String raison = rs.getString("raison"); |
| 62 | + envoyer = new Envoyer(idEnv, numEnvoyeur, numRecepteur, montant, date, raison); |
61 | 63 | }
|
62 |
| - return envoyer; |
63 | 64 | }
|
| 65 | + return envoyer; |
| 66 | + } |
64 | 67 |
|
65 |
| - public void updateEnvoyer(Envoyer envoyer) throws SQLException { |
66 |
| - try (Connection conn = connect(); |
67 |
| - PreparedStatement pstmt = conn.prepareStatement("UPDATE ENVOYER SET numEnvoyeur = ?, numRecepteur = ?, montant = ?, raison = ? WHERE idEnv = ?")) { |
68 |
| - pstmt.setString(1, envoyer.getNumEnvoyeur()); |
69 |
| - pstmt.setString(2, envoyer.getNumRecepteur()); |
70 |
| - pstmt.setInt(3, envoyer.getMontant()); |
71 |
| - pstmt.setString(4, envoyer.getRaison()); |
72 |
| - pstmt.setInt(5, envoyer.getIdEnv()); |
73 |
| - pstmt.executeUpdate(); |
74 |
| - } |
| 68 | + public void updateEnvoyer(Envoyer envoyer) throws SQLException { |
| 69 | + try (Connection conn = connect(); PreparedStatement pstmt = conn.prepareStatement("UPDATE ENVOYER SET numEnvoyeur = ?, numRecepteur = ?, montant = ?, raison = ? WHERE idEnv = ?")) { |
| 70 | + pstmt.setString(1, envoyer.numEnvoyeur()); |
| 71 | + pstmt.setString(2, envoyer.numRecepteur()); |
| 72 | + pstmt.setInt(3, envoyer.montant()); |
| 73 | + pstmt.setString(4, envoyer.raison()); |
| 74 | + pstmt.setInt(5, envoyer.idEnv()); |
| 75 | + pstmt.executeUpdate(); |
75 | 76 | }
|
| 77 | + } |
76 | 78 |
|
77 |
| - public void deleteEnvoyer(int idEnv) throws SQLException { |
78 |
| - try (Connection conn = connect(); |
79 |
| - PreparedStatement pstmt = conn.prepareStatement("DELETE FROM ENVOYER WHERE idEnv = ?")) { |
80 |
| - pstmt.setInt(1, idEnv); |
81 |
| - pstmt.executeUpdate(); |
82 |
| - } |
| 79 | + public void deleteEnvoyer(int idEnv) throws SQLException { |
| 80 | + try (Connection conn = connect(); |
| 81 | + PreparedStatement pstmt = conn.prepareStatement("DELETE FROM ENVOYER WHERE idEnv = ?")) { |
| 82 | + pstmt.setInt(1, idEnv); |
| 83 | + pstmt.executeUpdate(); |
83 | 84 | }
|
84 | 85 | }
|
| 86 | +} |
0 commit comments