Question a) – Implémentation de la couche Données (10 pts)
Il faut créer les méthodes de gestion de base de données dans un DAO (Data Access
Object). Voici une proposition en Java.
✅ Classe [Link] (modèle)
java
CopierModifier
public class Client {
private int id;
private String nom;
private String prenom;
private String sexe;
private String dateNaissance;
private String email;
private String telephone;
// Constructeurs
public Client(int id, String nom, String prenom, String sexe, String
dateNaissance, String email, String telephone) {
[Link] = id;
[Link] = nom;
[Link] = prenom;
[Link] = sexe;
[Link] = dateNaissance;
[Link] = email;
[Link] = telephone;
}
// Getters et Setters
public int getId() { return id; }
public void setId(int id) { [Link] = id; }
public String getNom() { return nom; }
public void setNom(String nom) { [Link] = nom; }
public String getPrenom() { return prenom; }
public void setPrenom(String prenom) { [Link] = prenom; }
public String getSexe() { return sexe; }
public void setSexe(String sexe) { [Link] = sexe; }
public String getDateNaissance() { return dateNaissance; }
public void setDateNaissance(String dateNaissance) { [Link]
= dateNaissance; }
public String getEmail() { return email; }
public void setEmail(String email) { [Link] = email; }
public String getTelephone() { return telephone; }
public void setTelephone(String telephone) { [Link] =
telephone; }
}
✅ Classe [Link] (accès aux données)
java
CopierModifier
import [Link].*;
import [Link].*;
public class ClientDAO {
private Connection con;
public ClientDAO() {
try {
[Link]("[Link]");
con =
[Link]("jdbc:mysql://localhost:3306/gestion_clients",
"root", "");
} catch (Exception e) {
[Link]();
}
}
// Ajouter un client
public void addClient(Client client) throws SQLException {
String sql = "INSERT INTO client(nom, prenom, sexe, dateNaissance,
email, telephone) VALUES (?, ?, ?, ?, ?, ?)";
PreparedStatement pst = [Link](sql);
[Link](1, [Link]());
[Link](2, [Link]());
[Link](3, [Link]());
[Link](4, [Link]());
[Link](5, [Link]());
[Link](6, [Link]());
[Link]();
}
// Supprimer un client par ID
public void deleteClient(int id) throws SQLException {
PreparedStatement pst = [Link]("DELETE FROM client
WHERE id = ?");
[Link](1, id);
[Link]();
}
// Modifier un client
public void updateClient(Client client) throws SQLException {
String sql = "UPDATE client SET nom=?, prenom=?, sexe=?,
dateNaissance=?, email=?, telephone=? WHERE id=?";
PreparedStatement pst = [Link](sql);
[Link](1, [Link]());
[Link](2, [Link]());
[Link](3, [Link]());
[Link](4, [Link]());
[Link](5, [Link]());
[Link](6, [Link]());
[Link](7, [Link]());
[Link]();
}
// Vérifier si client existe par ID
public boolean isNew(int id) throws SQLException {
PreparedStatement pst = [Link]("SELECT * FROM client
WHERE id = ?");
[Link](1, id);
ResultSet rs = [Link]();
return ![Link](); // S'il n’existe pas => c’est un nouveau
}
// Lister tous les clients
public List<Client> getList() throws SQLException {
List<Client> clients = new ArrayList<>();
Statement st = [Link]();
ResultSet rs = [Link]("SELECT * FROM client");
while ([Link]()) {
[Link](new Client(
[Link]("id"),
[Link]("nom"),
[Link]("prenom"),
[Link]("sexe"),
[Link]("dateNaissance"),
[Link]("email"),
[Link]("telephone")
));
}
return clients;
}
// Compter les clients
public int count() throws SQLException {
Statement st = [Link]();
ResultSet rs = [Link]("SELECT COUNT(*) AS total FROM
client");
[Link]();
return [Link]("total");
}
}
Question b) – Implémentation de la couche Présentation (10 pts)
Ici, on te demande d’implémenter un formulaire d’ajout de client. On va faire simple avec
Swing :
java
CopierModifier
import [Link].*;
import [Link].*;
public class ClientForm extends JFrame {
private JTextField nomField, prenomField, emailField, telField,
dateField, sexeField;
private JButton btnAjouter;
private ClientDAO clientDAO;
public ClientForm() {
clientDAO = new ClientDAO();
setTitle("Ajout Client");
setSize(400, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(null);
JLabel lblNom = new JLabel("Nom:");
[Link](10, 10, 100, 25);
add(lblNom);
nomField = new JTextField();
[Link](120, 10, 200, 25);
add(nomField);
JLabel lblPrenom = new JLabel("Prénom:");
[Link](10, 40, 100, 25);
add(lblPrenom);
prenomField = new JTextField();
[Link](120, 40, 200, 25);
add(prenomField);
JLabel lblSexe = new JLabel("Sexe:");
[Link](10, 70, 100, 25);
add(lblSexe);
sexeField = new JTextField();
[Link](120, 70, 200, 25);
add(sexeField);
JLabel lblDate = new JLabel("Date Naiss.:");
[Link](10, 100, 100, 25);
add(lblDate);
dateField = new JTextField();
[Link](120, 100, 200, 25);
add(dateField);
JLabel lblEmail = new JLabel("Email:");
[Link](10, 130, 100, 25);
add(lblEmail);
emailField = new JTextField();
[Link](120, 130, 200, 25);
add(emailField);
JLabel lblTel = new JLabel("Téléphone:");
[Link](10, 160, 100, 25);
add(lblTel);
telField = new JTextField();
[Link](120, 160, 200, 25);
add(telField);
btnAjouter = new JButton("Ajouter");
[Link](120, 200, 100, 30);
add(btnAjouter);
[Link](new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Client client = new Client(
0,
[Link](),
[Link](),
[Link](),
[Link](),
[Link](),
[Link]()
);
[Link](client);
[Link](null, "Client ajouté !");
} catch (Exception ex) {
[Link]();
}
}
});
setVisible(true);
}
public static void main(String[] args) {
new ClientForm();
}
}
Question c) – Diagramme de séquence
Section 2 :
Question b) – Écrire une requête SQL qui affiche les clients dont l’âge est compris entre 45 ans et 55
ans
SELECT*FROM CLIENT WHERE AGE BETWEN 45 AND 55 ;
Question c) – Écrire la requête SQL qui permet d’ajouter le champ villeResidence dans la table
client sans perdre les données
ALTER TABLE client
ADD villeResidence VARCHAR(100);
Partie 2 – POO :