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

38
bibliotheque.sql Normal file
View File

@ -0,0 +1,38 @@
-- Création de la base
DROP DATABASE IF EXISTS bibliotheque;
CREATE DATABASE bibliotheque;
USE bibliotheque;
-- Table des clients
CREATE TABLE Clients (
id_client INT AUTO_INCREMENT PRIMARY KEY,
nom VARCHAR(50) NOT NULL,
prenom VARCHAR(50) NOT NULL
);
-- Table des livres
CREATE TABLE Livres (
id_livre INT AUTO_INCREMENT PRIMARY KEY,
titre VARCHAR(200) NOT NULL,
auteur VARCHAR(100) NOT NULL,
disponible BOOLEAN NOT NULL DEFAULT TRUE
);
-- Table des emprunts
CREATE TABLE Emprunts (
id_emprunt INT AUTO_INCREMENT PRIMARY KEY,
id_client INT NOT NULL,
id_livre INT NOT NULL,
date_emprunt DATE NOT NULL,
date_retour DATE DEFAULT NULL,
FOREIGN KEY (id_client) REFERENCES Clients(id_client),
FOREIGN KEY (id_livre) REFERENCES Livres(id_livre)
);
-- Quelques données de test
INSERT INTO Clients (nom, prenom)
VALUES ('Dupont','Jean');
INSERT INTO Livres (titre, auteur)
VALUES ('1984','George Orwell'),
('Le Petit Prince','Antoine de Saint-Exupéry');

3
compile.bat Executable file
View File

@ -0,0 +1,3 @@
call mvn clean
call mvn package -e
java -jar .\target\gestion-bibliotheque-1.0.jar

1
creation_bdd.bat Executable file
View File

@ -0,0 +1 @@
mysql -u root -p --execute="source bibliotheque.sql"

View File

@ -0,0 +1,67 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ic-epsi</groupId>
<artifactId>gestion-bibliotheque</artifactId>
<version>1.0</version>
<build>
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<archive>
<manifest>
<mainClass>Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<artifactId>maven-shade-plugin</artifactId>
<version>3.4.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer>
<mainClass>Main</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.10.0</version>
<scope>test</scope>
<exclusions>
<exclusion>
<artifactId>junit-jupiter-api</artifactId>
<groupId>org.junit.jupiter</groupId>
</exclusion>
<exclusion>
<artifactId>junit-jupiter-params</artifactId>
<groupId>org.junit.jupiter</groupId>
</exclusion>
<exclusion>
<artifactId>junit-jupiter-engine</artifactId>
<groupId>org.junit.jupiter</groupId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<properties>
<maven.compiler.target>17</maven.compiler.target>
<maven.compiler.source>17</maven.compiler.source>
</properties>
</project>

73
pom.xml Normal file
View File

@ -0,0 +1,73 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ic-epsi</groupId>
<artifactId>gestion-bibliotheque</artifactId>
<version>1.0</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>
<!-- dépendance MySQL -->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.0.33</version>
</dependency>
<!-- Dépendance JUnit 5 -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.10.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Plugin pour créer un JAR exécutable avec la classe Main -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<archive>
<manifest>
<mainClass>Main</mainClass> <!-- Sans package -->
</manifest>
</archive>
</configuration>
</plugin>
<!-- Fat-jar avec dépendances -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.4.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>Main</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

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();
}
}