Liste client +
Connexion Bdd + Ajout d'un client
This commit is contained in:
110
src/main/java/com/test/bdd/ClientMysql.java
Normal file
110
src/main/java/com/test/bdd/ClientMysql.java
Normal file
@@ -0,0 +1,110 @@
|
||||
/*
|
||||
* 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.Client;
|
||||
import java.sql.PreparedStatement;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author emile.malcuit
|
||||
*/
|
||||
public class ClientMysql {
|
||||
private Connection laConnexion = connexionSQL.getConnect("192.168.100.100", "bdclient", "adminClient", "admin");
|
||||
|
||||
public ClientMysql() {
|
||||
}
|
||||
|
||||
public Connection getLaConnexion() {
|
||||
return laConnexion;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void createClient(Client 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<Client> readAll(){
|
||||
ArrayList<Client> lesClients=new ArrayList<>();
|
||||
try {
|
||||
Statement stmt=laConnexion.createStatement();
|
||||
ResultSet resultQ=null;
|
||||
resultQ=stmt.executeQuery("SELECT * FROM client;");
|
||||
while (resultQ.next()){
|
||||
Client resultat=new Client(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 Client read(int id){
|
||||
String commande = "SELECT * FROM client WHERE id = ?";
|
||||
Client recherche = new Client();
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user