This commit is contained in:
Awen Lelu
2025-11-14 10:13:20 +01:00
commit fafa1a27c5
13 changed files with 555 additions and 0 deletions

45
src/main/java/Main.java Normal file
View File

@@ -0,0 +1,45 @@
import java.sql.Connection;
import java.sql.SQLException;
import java.time.LocalDate;
public class Main {
public static void main(String[] args) {
//bibliotheque.Livre livre = new bibliotheque.Livre("1984", "George Orwell");
System.out.println("Bonjour !");
try (Connection conn = bibliotheque.BDDbilbio.getConnection()) {
System.out.println("Connexion réussie à la base de données !");
// Ajout d'un livre
System.out.println("=====Ajout d'un livre=====");
bibliotheque.Livre livre = new bibliotheque.Livre(conn);
livre.addToBDD("Le petit prince", "Antoine de Saint Exupéry");
livre.afficherTous();
// Ajout d'un client
System.out.println("=====Ajout d'un client=====");
bibliotheque.Client client = new bibliotheque.Client(conn);
client.addToBDD("Nicolas", "Zaimeche");
client.afficherTous();
System.out.println("=====Ajout d'un emprunt=====");
bibliotheque.Emprunt emprunt = new bibliotheque.Emprunt(conn);
Integer id_livre = livre.findId("Le petit prince", "Antoine de Saint Exupéry");
Integer id_client = client.findId("Nicolas", "Zaimeche");;
emprunt.addToBDD(id_client, id_livre, LocalDate.now(), 4);
emprunt.afficherTous();
emprunt.deleteByLivre(id_livre);
emprunt.afficherTous();
livre.deleteFromBDD("Le petit prince", "Antoine de Saint Exupéry");
livre.afficherTous();
client.deleteFromBDD("Nicolas", "Zaimeche");
client.afficherTous();
} catch (SQLException e) {
System.out.println("Erreur de connexion : " + e.getMessage());
e.printStackTrace();
}
}
}

View File

@@ -0,0 +1,20 @@
package bibliotheque;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class BDDbilbio {
private static final String URL = "";
private static final String USER = "";
private static final String PASSWORD = "";
public static Connection getConnection() throws SQLException {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return DriverManager.getConnection(URL, USER, PASSWORD);
}
}

View File

@@ -0,0 +1,93 @@
package bibliotheque;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Client {
// Classe pour interagir avec la table Clients
private Connection conn;
public Client(Connection conn)
{
this.conn = conn;
}
public void addToBDD(String nom, String prenom)
{
// Ajout d'un Client à la bilbiothèque
String sql = "INSERT INTO Clients(nom, prenom) VALUES (?, ?)";
try (PreparedStatement stmt = this.conn.prepareStatement(sql)) {
stmt.setString(1, nom);
stmt.setString(2, prenom);
stmt.executeUpdate();
System.out.println("Client ajouté : " + nom);
} catch (SQLException e) {
e.printStackTrace();
}
}
public void afficherTous()
{
String sql = "SELECT id_client, nom, prenom FROM Clients";
try (Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql)) {
System.out.println("Liste des Clients :");
while (rs.next()) {
int id = rs.getInt("id_client");
String nom = rs.getString("nom");
String prenom = rs.getString("prenom");
System.out.println(id + " | " + nom + " | " + prenom);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public void deleteFromBDD(String nom, String prenom)
{
String sql = "DELETE FROM clients WHERE nom = ? AND prenom = ?";
try (PreparedStatement stmt = this.conn.prepareStatement(sql)) {
stmt.setString(1, nom);
stmt.setString(2, prenom);
int rows = stmt.executeUpdate();
if (rows > 0) {
System.out.println("Client supprimé : " + nom + " " + prenom);
} else {
System.out.println("Aucun client trouvé avec ce nom/prenom.");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public Integer findId(String nom, String prenom)
{
String sql = "SELECT id_client FROM Clients WHERE nom = ? AND prenom = ?";
try (PreparedStatement stmt = this.conn.prepareStatement(sql)) {
stmt.setString(1, nom);
stmt.setString(2, prenom);
try (ResultSet rs = stmt.executeQuery()) {
if (rs.next()) {
return rs.getInt("id_client");
} else {
System.out.println("Aucun client trouvé pour : " + prenom + " " + nom);
return null;
}
}
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
}

View File

@@ -0,0 +1,77 @@
package bibliotheque;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Date;
public class Emprunt {
// Classe pour interagir avec la table Emprunts
private Connection conn;
public Emprunt(Connection conn)
{
this.conn = conn;
}
public void addToBDD(int idClient, int idLivre, java.time.LocalDate dateEmprunt, int dureeSemaines)
{
String addEmpruntSql = "INSERT INTO Emprunts(id_client, id_livre, date_emprunt, date_retour) VALUES (?, ?, ?, ?)";
String updateLivreSql = "UPDATE Livres SET disponible = FALSE WHERE id_livre = ?";
try {
PreparedStatement stmtEmprunt = conn.prepareStatement(addEmpruntSql);
stmtEmprunt.setInt(1, idClient);
stmtEmprunt.setInt(2, idLivre);
stmtEmprunt.setDate(3, Date.valueOf(dateEmprunt)); // conversion LocalDate -> SQL Date
stmtEmprunt.setDate(4, Date.valueOf(dateEmprunt.plusWeeks(dureeSemaines)));
stmtEmprunt.executeUpdate();
System.out.println("Nouvel emprunt ajouté : client " + idClient + ", livre " + idLivre);
PreparedStatement stmtDisponible = conn.prepareStatement(updateLivreSql);
stmtDisponible.setInt(1, idLivre);
stmtDisponible.executeUpdate();
}
catch (SQLException e) {
e.printStackTrace();
}
}
public void afficherTous() {
String sql = "SELECT id_emprunt, id_client, id_livre, date_emprunt, date_retour FROM Emprunts";
try (PreparedStatement stmt = conn.prepareStatement(sql);
ResultSet rs = stmt.executeQuery()) {
System.out.println("=== Liste des emprunts ===");
while (rs.next()) {
int idEmprunt = rs.getInt("id_emprunt");
int idClient = rs.getInt("id_client");
int idLivre = rs.getInt("id_livre");
java.sql.Date dateEmprunt = rs.getDate("date_emprunt");
java.sql.Date dateRetour = rs.getDate("date_retour");
System.out.println(
"Emprunt #" + idEmprunt +
" | Client: " + idClient +
" | Livre: " + idLivre +
" | Date emprunt: " + dateEmprunt +
" | Date retour: " + (dateRetour != null ? dateRetour : "Non rendu")
);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public void deleteByLivre(Integer idLivre) throws SQLException {
String sql = "DELETE FROM emprunts WHERE id_livre = ?";
try (PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setInt(1, idLivre);
ps.executeUpdate();
}
}
}

View File

@@ -0,0 +1,90 @@
package bibliotheque;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Livre {
// Classe pour interagir avec la table Livres
private Connection conn;
public Livre(Connection conn)
{
this.conn = conn;
}
public void addToBDD(String titre, String auteur) {
// Ajout d'un livre à la bilbiothèque
String sql = "INSERT INTO livres(titre, auteur) VALUES (?, ?)";
try (PreparedStatement stmt = this.conn.prepareStatement(sql)) {
stmt.setString(1, titre);
stmt.setString(2, auteur);
stmt.executeUpdate();
System.out.println("Livre ajouté : " + titre);
} catch (SQLException e) {
e.printStackTrace();
}
}
public void afficherTous() {
String sql = "SELECT id_livre, titre, auteur FROM livres";
try (Statement stmt = this.conn.createStatement();
ResultSet rs = stmt.executeQuery(sql)) {
System.out.println("Liste des livres :");
while (rs.next()) {
int id = rs.getInt("id_livre");
String titre = rs.getString("titre");
String auteur = rs.getString("auteur");
System.out.println(id + " | " + titre + " | " + auteur);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public void deleteFromBDD(String titre, String auteur) {
String sql = "DELETE FROM livres WHERE titre = ? AND auteur = ?";
try (PreparedStatement stmt = this.conn.prepareStatement(sql)) {
stmt.setString(1, titre);
stmt.setString(2, auteur);
int rows = stmt.executeUpdate();
if (rows > 0) {
System.out.println("Livre supprimé : " + titre + " de " + auteur);
} else {
System.out.println("Aucun livre trouvé avec ce titre/auteur.");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public Integer findId(String titre, String auteur)
{
String sql = "SELECT id_livre FROM Livres WHERE titre = ? AND auteur = ?";
try (PreparedStatement stmt = this.conn.prepareStatement(sql)) {
stmt.setString(1, titre);
stmt.setString(2, auteur);
try (ResultSet rs = stmt.executeQuery()) {
if (rs.next()) {
return rs.getInt("id_livre");
} else {
System.out.println("Aucun livre trouvé pour : " + titre + " " + auteur);
return null;
}
}
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
}

View File

@@ -0,0 +1,3 @@
public class ClientTest {
}

View File

@@ -0,0 +1,3 @@
public class EmpruntTest {
}

View File

@@ -0,0 +1,42 @@
package bibliotheque;
import org.junit.jupiter.api.*;
import java.sql.Connection;
import java.sql.SQLException;
import static org.junit.jupiter.api.Assertions.*;
public class LivreTest {
private static Connection conn;
private Livre livre;
@BeforeAll
static void initConnection() throws SQLException {
conn = bibliotheque.BDDbilbio.getConnection();
assertNotNull(conn, "Connexion à la BDD échouée !");
}
@BeforeEach
void setup() {
livre = new Livre(conn);
}
@Test
void testAddAndFindLivre() throws SQLException {
livre.addToBDD("TestLivre", "AuteurTest");
Integer id = livre.findId("TestLivre", "AuteurTest");
assertNotNull(id, "Le livre n'a pas été trouvé après insertion !");
}
@Test
void testDeleteLivre() throws SQLException {
livre.deleteFromBDD("TestLivre", "AuteurTest");
Integer id = livre.findId("TestLivre", "AuteurTest");
assertNull(id, "Le livre devrait avoir été supprimé !");
}
@AfterAll
static void closeConn() throws SQLException {
conn.close();
}
}