109 lines
3.5 KiB
Java
109 lines
3.5 KiB
Java
/*
|
|
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
|
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
|
|
*/
|
|
package com.test.bdd;
|
|
|
|
import java.sql.Connection;
|
|
import java.sql.ResultSet;
|
|
import java.sql.SQLException;
|
|
import java.sql.Statement;
|
|
import java.util.ArrayList;
|
|
import com.test.beans.ClientOld;
|
|
import java.sql.PreparedStatement;
|
|
|
|
/**
|
|
*
|
|
* @author emile.malcuit
|
|
*/
|
|
public class ClientMysql {
|
|
private Connection laConnexion = connexionSQL.getConnect("192.168.100.100", "bdclient", "adminClient", "mdpBDclient");
|
|
public ClientMysql() {
|
|
}
|
|
|
|
public Connection getLaConnexion() {
|
|
return laConnexion;
|
|
}
|
|
|
|
|
|
public void createClient(ClientOld newCli) {
|
|
String commande = "INSERT INTO client(nom, prenom, mail) VALUES (?, ?, ?)";
|
|
|
|
try (PreparedStatement pstmt = laConnexion.prepareStatement(commande)) {
|
|
pstmt.setString(1, newCli.getNom());
|
|
pstmt.setString(2, newCli.getPrenom());
|
|
pstmt.setString(3, newCli.getMail());
|
|
|
|
int rowsInserted = pstmt.executeUpdate();
|
|
|
|
if (rowsInserted > 0) {
|
|
System.out.println("Client inséré avec succès !");
|
|
}
|
|
} catch (SQLException ex) {
|
|
System.err.println("Erreur lors de l'insertion du client : " + ex.getMessage());
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public ArrayList<ClientOld> readAll(){
|
|
ArrayList<ClientOld> lesClients=new ArrayList<>();
|
|
try {
|
|
Statement stmt=laConnexion.createStatement();
|
|
ResultSet resultQ=null;
|
|
resultQ=stmt.executeQuery("SELECT * FROM client;");
|
|
while (resultQ.next()){
|
|
ClientOld resultat=new ClientOld(resultQ.getInt("id"),resultQ.getString("nom"),resultQ.getString("prenom"),resultQ.getString("mail"));
|
|
lesClients.add(resultat);
|
|
}
|
|
resultQ.close();
|
|
stmt.close();
|
|
|
|
}
|
|
catch(SQLException ex){
|
|
System.out.println("SQLException:"+ex.getMessage());
|
|
System.out.println("SQLState:"+ex.getSQLState());
|
|
System.out.println("code Erreur:"+ex.getErrorCode());
|
|
}
|
|
return lesClients;
|
|
}
|
|
|
|
|
|
public ClientOld read(int id){
|
|
String commande = "SELECT * FROM client WHERE id = ?";
|
|
ClientOld recherche = new ClientOld();
|
|
|
|
try(PreparedStatement pstmt = laConnexion.prepareStatement(commande)){
|
|
pstmt.setString(1, Integer.toString(id));
|
|
ResultSet resultQ = pstmt.executeQuery();
|
|
recherche.setId(resultQ.getInt("id"));
|
|
recherche.setNom(resultQ.getString("nom"));
|
|
recherche.setPrenom(resultQ.getString("prenom"));
|
|
recherche.setMail(resultQ.getString("mail"));
|
|
|
|
} catch (SQLException ex) {
|
|
System.err.println("Erreur lors de la recherche du client : " + ex.getMessage());
|
|
}
|
|
|
|
return recherche;
|
|
}
|
|
|
|
|
|
public void delete(int id){
|
|
String commande = "DELETE FROM client WHERE id = ?";
|
|
|
|
try(PreparedStatement pstmt = laConnexion.prepareStatement(commande)){
|
|
pstmt.setString(1, Integer.toString(id));
|
|
ResultSet resultQ = pstmt.executeQuery();
|
|
|
|
}catch(SQLException ex){
|
|
System.err.println("Erreur lors de la suppression du client : " + ex.getMessage());
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|