UsflLinks

Télécharger au format docx, pdf ou txt
Télécharger au format docx, pdf ou txt
Vous êtes sur la page 1sur 4

https://elbahihassan.

com/programmation-orientee-objet-en-c/

https://elbahihassan.com/blog-posts/

https://elbahihassan.com/analyse-et-conception-orientee-objet-uml/

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class Livre {


private int isbn;
private String titre;
private String dateParution;
private int nbExemplaire;
private String auteur;
private String domaine;

public Livre() {
}

public Livre(int isbn, String titre, String dateParution, int nbExemplaire,


String auteur, String domaine) {
this.isbn = isbn;
this.titre = titre;
this.dateParution = dateParution;
this.nbExemplaire = nbExemplaire;
this.auteur = auteur;
this.domaine = domaine;
}

public void ajouter(Livre l) {


try (Connection conn = ConnexionBD.seconnecter();
Statement stm = conn.createStatement()) {
String query = "INSERT INTO livre (isbn, titre, date_parution,
nb_exemplaire, auteur, domaine) VALUES (" +
l.isbn + ", '" + l.titre + "', '" + l.dateParution + "', " +
l.nbExemplaire + ", '" +
l.auteur + "', '" + l.domaine + "')";
stm.executeUpdate(query);
} catch (Exception ex) {
ex.printStackTrace();
}
}

public void supprimer(int isbn) {


try (Connection conn = ConnexionBD.seconnecter();
Statement stm = conn.createStatement()) {
String query = "DELETE FROM livre WHERE isbn=" + isbn;
stm.executeUpdate(query);
} catch (Exception e) {
e.printStackTrace();
}
}

public void modifier(int isbn, String titre, String dateParution, int


nbExemplaire, String auteur, String domaine) {
try (Connection conn = ConnexionBD.seconnecter();
Statement stm = conn.createStatement()) {
String query = "UPDATE livre SET titre='" + titre + "',
date_parution='" + dateParution +
"', nb_exemplaire=" + nbExemplaire + ", auteur='" + auteur
+ "', domaine='" +
domaine + "' WHERE isbn=" + isbn;
stm.executeUpdate(query);
} catch (Exception e) {
e.printStackTrace();
}
}

public List<Livre> getAllLivres() {


List<Livre> listLivre = new ArrayList<>();
try (Connection conn = ConnexionBD.seconnecter();
Statement stm = conn.createStatement();
ResultSet rs = stm.executeQuery("SELECT * FROM livre")) {
while (rs.next()) {
int isbn1 = rs.getInt("isbn");
String titre1 = rs.getString("titre");
String dateParution1 = rs.getString("date_parution");
int nbExemplaire1 = rs.getInt("nb_exemplaire");
String auteur1 = rs.getString("auteur");
String domaine1 = rs.getString("domaine");
listLivre.add(new Livre(isbn1, titre1, dateParution1,
nbExemplaire1, auteur1, domaine1));
}
} catch (Exception e) {
e.printStackTrace();
}
return listLivre;
}

public List<Livre> afficherParIsbn(int isbn) {


List<Livre> listLivre = new ArrayList<>();
try (Connection conn = ConnexionBD.seconnecter();
Statement stm = conn.createStatement();
ResultSet rs = stm.executeQuery("SELECT * FROM livre WHERE
isbn=" + isbn)) {
while (rs.next()) {
int isbn1 = rs.getInt("isbn");
String titre1 = rs.getString("titre");
String dateParution1 = rs.getString("date_parution");
int nbExemplaire1 = rs.getInt("nb_exemplaire");
String auteur1 = rs.getString("auteur");
String domaine1 = rs.getString("domaine");
listLivre.add(new Livre(isbn1, titre1, dateParution1,
nbExemplaire1, auteur1, domaine1));
}
} catch (Exception e) {
e.printStackTrace();
}
return listLivre;
}
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class ConnexionBD {


private static Connection connexion = null;
private static final String URL = "jdbc:mysql://localhost:3306/biblio";
private static final String USER = "root";
private static final String PASSWORD = "";

public static Connection seconnecter() {


try {
if (connexion == null || connexion.isClosed()) {
Class.forName("com.mysql.cj.jdbc.Driver");
connexion = DriverManager.getConnection(URL, USER,
PASSWORD);
}
} catch (Exception e) {
e.printStackTrace();
}
return connexion;
}

public static void seDeconnecter() {


try {
if (connexion != null && !connexion.isClosed()) {
connexion.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}

Vous aimerez peut-être aussi