Skip to content

Commit 1d10e1f

Browse files
committed
Minor structure fixes
- fix class name to start with uppercase - change POJOs into records - move DB connection config to config.properties (not versioned) - add DB tables creation file - use jakarta and jsp 2.0.0 instead of javax
1 parent 2f65899 commit 1d10e1f

23 files changed

+317
-391
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,6 @@ gradle-app.setting
5050
# JDT-specific (Eclipse Java Development Tools)
5151
.classpath
5252

53+
# Config files
54+
**/config.properties
55+

app/build.gradle

+13-5
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,15 @@ repositories {
1818
}
1919

2020
dependencies {
21-
// This dependency is used by the application.
22-
implementation libs.guava
23-
compileOnly 'javax.servlet:javax.servlet-api:4.0.1'
24-
compileOnly 'javax.servlet.jsp:jsp-api:2.2'
25-
implementation 'javax.servlet:jstl:1.2'
21+
implementation 'jakarta.servlet:jakarta.servlet-api:4.0.3'
22+
implementation 'jakarta.servlet.jsp:jakarta.servlet.jsp-api:2.3.6'
23+
implementation 'org.glassfish.web:jakarta.servlet.jsp.jstl:2.0.0'
24+
25+
// Add JUnit Jupiter API for writing tests
26+
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2'
27+
28+
// Add JUnit Jupiter Engine for running tests
29+
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.2'
2630
}
2731

2832
sourceSets {
@@ -33,6 +37,10 @@ sourceSets {
3337
}
3438
}
3539

40+
test {
41+
useJUnitPlatform()
42+
}
43+
3644
// Apply a specific Java toolchain to ease working on different environments.
3745
java {
3846
toolchain {

app/db/E-CASH.sql

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
CREATE TABLE CLIENT (
2+
numtel VARCHAR(20) PRIMARY KEY,
3+
nom VARCHAR(50),
4+
sexe VARCHAR(10),
5+
pays VARCHAR(50),
6+
solde INT,
7+
mail VARCHAR(100)
8+
);
9+
10+
CREATE TABLE TAUX (
11+
idtaux SERIAL PRIMARY KEY,
12+
montant1 INT,
13+
montant2 INT
14+
);
15+
16+
CREATE TABLE FRAIS (
17+
idfrais SERIAL PRIMARY KEY,
18+
montant1 INT,
19+
montant2 INT,
20+
frais INT
21+
);
22+
23+
CREATE TABLE ENVOYER (
24+
idEnv SERIAL PRIMARY KEY,
25+
numEnvoyeur VARCHAR(20) REFERENCES CLIENT(numtel),
26+
numRecepteur VARCHAR(20) REFERENCES CLIENT(numtel),
27+
montant INT,
28+
date TIMESTAMP,
29+
raison VARCHAR(255)
30+
);

app/src/main/java/org/example/DAO/ClientDao.java

+28-27
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
1-
package org.example.DAO;
1+
package org.example.dao;
2+
3+
import org.example.models.Client;
4+
import org.example.util.Config;
25

3-
import org.example.models.client;
46
import java.sql.*;
57
import java.util.ArrayList;
68
import java.util.List;
79

810
public class ClientDao {
911
private Connection connect() throws SQLException {
10-
String url = "jdbc:postgresql://localhost:5432/cash";
11-
String user = "postgres";
12-
String password = "mario123";
12+
String url = Config.get("db.url");
13+
String user = Config.get("db.user");
14+
String password = Config.get("db.password");
1315
return DriverManager.getConnection(url, user, password);
1416
}
1517

16-
public List<client> listAllClients() throws SQLException {
17-
List<client> listClients = new ArrayList<>();
18+
public List<Client> listAllClients() throws SQLException {
19+
List<Client> listClients = new ArrayList<>();
1820
try (Connection conn = connect();
1921
Statement stmt = conn.createStatement();
2022
ResultSet rs = stmt.executeQuery("SELECT * FROM CLIENT")) {
@@ -25,27 +27,26 @@ public List<client> listAllClients() throws SQLException {
2527
String pays = rs.getString("pays");
2628
int solde = rs.getInt("solde");
2729
String mail = rs.getString("mail");
28-
listClients.add(new client(numtel, nom, sexe, pays, solde, mail));
30+
listClients.add(new Client(numtel, nom, sexe, pays, solde, mail));
2931
}
3032
}
3133
return listClients;
3234
}
3335

34-
public void insertClient(client client) throws SQLException {
35-
try (Connection conn = connect();
36-
PreparedStatement pstmt = conn.prepareStatement("INSERT INTO CLIENT (numtel, nom, sexe, pays, solde, mail) VALUES (?, ?, ?, ?, ?, ?)")) {
37-
pstmt.setString(1, client.getNumtel());
38-
pstmt.setString(2, client.getNom());
39-
pstmt.setString(3, client.getSexe());
40-
pstmt.setString(4, client.getPays());
41-
pstmt.setInt(5, client.getSolde());
42-
pstmt.setString(6, client.getMail());
36+
public void insertClient(Client client) throws SQLException {
37+
try (Connection conn = connect(); PreparedStatement pstmt = conn.prepareStatement("INSERT INTO CLIENT (numtel, nom, sexe, pays, solde, mail) VALUES (?, ?, ?, ?, ?, ?)")) {
38+
pstmt.setString(1, client.numtel());
39+
pstmt.setString(2, client.nom());
40+
pstmt.setString(3, client.sexe());
41+
pstmt.setString(4, client.pays());
42+
pstmt.setInt(5, client.solde());
43+
pstmt.setString(6, client.mail());
4344
pstmt.executeUpdate();
4445
}
4546
}
4647

47-
public client getClient(String numtel) throws SQLException {
48-
client client = null;
48+
public Client getClient(String numtel) throws SQLException {
49+
Client client = null;
4950
try (Connection conn = connect();
5051
PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM CLIENT WHERE numtel = ?")) {
5152
pstmt.setString(1, numtel);
@@ -56,21 +57,21 @@ public client getClient(String numtel) throws SQLException {
5657
String pays = rs.getString("pays");
5758
int solde = rs.getInt("solde");
5859
String mail = rs.getString("mail");
59-
client = new client(numtel, nom, sexe, pays, solde, mail);
60+
client = new Client(numtel, nom, sexe, pays, solde, mail);
6061
}
6162
}
6263
return client;
6364
}
6465

65-
public void updateClient(client client) throws SQLException {
66+
public void updateClient(Client client) throws SQLException {
6667
try (Connection conn = connect();
6768
PreparedStatement pstmt = conn.prepareStatement("UPDATE CLIENT SET nom = ?, sexe = ?, pays = ?, solde = ?, mail = ? WHERE numtel = ?")) {
68-
pstmt.setString(1, client.getNom());
69-
pstmt.setString(2, client.getSexe());
70-
pstmt.setString(3, client.getPays());
71-
pstmt.setInt(4, client.getSolde());
72-
pstmt.setString(5, client.getMail());
73-
pstmt.setString(6, client.getNumtel());
69+
pstmt.setString(1, client.nom());
70+
pstmt.setString(2, client.sexe());
71+
pstmt.setString(3, client.pays());
72+
pstmt.setInt(4, client.solde());
73+
pstmt.setString(5, client.mail());
74+
pstmt.setString(6, client.numtel());
7475
pstmt.executeUpdate();
7576
}
7677
}
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,86 @@
1-
package org.example.DAO;
1+
package org.example.dao;
22

33
import org.example.models.Envoyer;
4+
import org.example.util.Config;
5+
46
import java.sql.*;
57
import java.util.ArrayList;
68
import java.util.List;
79

810
public class EnvoyerDao {
911

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+
}
1618

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));
3133
}
32-
return listEnvoyer;
3334
}
35+
return listEnvoyer;
36+
}
3437

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();
4547
}
48+
}
4649

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);
6163
}
62-
return envoyer;
6364
}
65+
return envoyer;
66+
}
6467

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();
7576
}
77+
}
7678

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();
8384
}
8485
}
86+
}

0 commit comments

Comments
 (0)